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

52
LINES

< > BotCompany Repo | #1002204 // dir2zip_recurse

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

Libraryless. Click here for Pure Java version (9845L/54K).

static bool dir2zip_recurse_verbose;

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

// TODO: the zero files case?
static int dir2zip_recurse(File inDir, File zip, String outPrefix) ctex {
  mkdirsForFile(zip);
  FileOutputStream fout = newFileOutputStream(zip);
  ZipOutputStream outZip = new ZipOutputStream(fout);
  try {
    ret dir2zip_recurse(inDir, outZip, outPrefix, 0);
  } finally {
    outZip.close();
  }
}

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

static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) ctex {
  if (++level >= 20) fail("woot? 20 levels in zip?");
  
  new L<File> files;
  for (File f : listFiles(inDir))
    files.add(f);

  int n = 0;
  sortFilesByName(files);
  for (File f : files) {
    if (f.isDirectory()) {
      if (dir2zip_recurse_verbose) print("dir2zip_recurse: Scanning " + f.getAbsolutePath());
      n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level);
    } else {
      if (dir2zip_recurse_verbose) print("Copying " + f.getName());
      outZip.putNextEntry(new ZipEntry(outPrefix + f.getName()));
      InputStream fin;
      try {
        fin = new FileInputStream(f);
      } catch e {
        print(e);
        continue;
      }
      temp fin;
      copyStream(fin, outZip);
      ++n;
    }
  }
  return n;
}

Author comment

Began life as a copy of #1001174

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002204
Snippet name: dir2zip_recurse
Eternal ID of this version: #1002204/4
Text MD5: 43f3f0c445a87d8a738922cdccb14109
Transpilation MD5: 55b694de151ce6d81fb719f5c2056d10
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-27 02:26:56
Source code size: 1487 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 636 / 1071
Version history: 3 change(s)
Referenced in: [show references]