Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

43
LINES

< > BotCompany Repo | #1005970 // saveGZStructureToFile

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (12572L/71K).

static long saveGZStructureToFile(S file, O o) {
  ret saveGZStructureToFile(getProgramFile(file), o);
}
  
// returns number of uncompressed bytes written
static long saveGZStructureToFile(File file, O o, structure_Data data default new) ctex {
  File parentFile = file.getParentFile();
  if (parentFile != null)
    parentFile.mkdirs();
  File tempFile = tempFileFor(file);
  if (tempFile.exists()) try {
    S saveName = tempFile.getPath() + ".saved." + now();
    copyFile(tempFile, new File(saveName));
  } catch (Throwable e) { printStackTrace(e); }
  
  FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
  CountingOutputStream cos;
  try {
    GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
    cos = new CountingOutputStream(gos);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(cos, "UTF-8");
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
    structureToPrintWriter(o, printWriter, data);
    printWriter.close();
    gos.close();
    fileOutputStream.close();
  } catch e {
    fileOutputStream.close();
    tempFile.delete();
    throw rethrow(e);
  }
  
  try {
    java.nio.file.Files.move(tempFile.toPath(), file.toPath(),
      java.nio.file.StandardCopyOption.REPLACE_EXISTING,
      java.nio.file.StandardCopyOption.ATOMIC_MOVE);
  } catch (java.nio.file.AtomicMoveNotSupportedException e) {
    java.nio.file.Files.move(tempFile.toPath(), file.toPath(),
      java.nio.file.StandardCopyOption.REPLACE_EXISTING);
  }
    
  ret cos.getFilePointer();
}

Author comment

Began life as a copy of #1005539

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005970
Snippet name: saveGZStructureToFile
Eternal ID of this version: #1005970/9
Text MD5: 7d94a32a03b001af262652e63a84e994
Transpilation MD5: df74a35947534442b093c232e0d04b86
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2026-08-10 01:20:22
Source code size: 1593 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 1155 / 1379
Version history: 8 change(s)
Referenced in: [show references]