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

52
LINES

< > BotCompany Repo | #1020482 // dir2zip_recurse_honorDontBackupFiles

JavaX fragment (include)

static bool dir2zip_recurse_honorDontBackupFiles_verbose;

static int dir2zip_recurse_honorDontBackupFiles(File inDir, File zip) {
  ret dir2zip_recurse_honorDontBackupFiles(inDir, zip, "");
}

// automatically adds a dummy file (empty.txt) in case there are no files to zip
static int dir2zip_recurse_honorDontBackupFiles(File inDir, File zip, String outPrefix) ctex {
  mkdirsForFile(zip);
  FileOutputStream fout = newFileOutputStream(zip);
  ZipOutputStream outZip = new ZipOutputStream(fout);
  try {
    int count = dir2zip_recurse_honorDontBackupFiles(inDir, outZip, outPrefix, 0);
    if (count == 0)
      zip_addDummyFile(outZip);
    ret count;
  } finally {
    outZip.close();
  }
}

static int dir2zip_recurse_honorDontBackupFiles(File inDir, ZipOutputStream outZip) {
  ret dir2zip_recurse_honorDontBackupFiles(inDir, outZip, "", 0);
}

static int dir2zip_recurse_honorDontBackupFiles(File inDir, ZipOutputStream outZip, String outPrefix, int level) ctex {
  if (++level >= 20) fail("woot? 20 levels in zip?");
  
  Set<S> dontBackup = asSet(tlft(loadTextFile(newFile(inDir, standardNameOfDontBackupFile()))));
  
  new L<File> files;
  for (File f : listFiles(inDir))
    if (!contains(dontBackup, f.getName()))
      files.add(f);

  int n = 0;
  sortFilesByName(files);
  for (File f : files) {
    if (f.isDirectory()) {
      print("dir2zip_recurse_honorDontBackupFiles: Scanning " + f.getAbsolutePath());
      n += dir2zip_recurse_honorDontBackupFiles(f, outZip, outPrefix + f.getName() + "/", level);
    } else {
      if (dir2zip_recurse_honorDontBackupFiles_verbose) print("Copying " + f.getName());
      outZip.putNextEntry(new ZipEntry(outPrefix + f.getName()));
      InputStream fin = new FileInputStream(f);
      copyStream(fin, outZip);
      fin.close();
      ++n;
    }
  }
  return n;
}

Author comment

Began life as a copy of #1002204

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020482
Snippet name: dir2zip_recurse_honorDontBackupFiles
Eternal ID of this version: #1020482/6
Text MD5: 3a8694ac5a0e072ebc3f1a9f06b4b965
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-04 11:36:26
Source code size: 1877 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 345 / 395
Version history: 5 change(s)
Referenced in: [show references]