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).

1  
static bool dir2zip_recurse_verbose;
2  
3  
static int dir2zip_recurse(File inDir, File zip) {
4  
  ret dir2zip_recurse(inDir, zip, "");
5  
}
6  
7  
// TODO: the zero files case?
8  
static int dir2zip_recurse(File inDir, File zip, String outPrefix) ctex {
9  
  mkdirsForFile(zip);
10  
  FileOutputStream fout = newFileOutputStream(zip);
11  
  ZipOutputStream outZip = new ZipOutputStream(fout);
12  
  try {
13  
    ret dir2zip_recurse(inDir, outZip, outPrefix, 0);
14  
  } finally {
15  
    outZip.close();
16  
  }
17  
}
18  
19  
static int dir2zip_recurse(File inDir, ZipOutputStream outZip) {
20  
  ret dir2zip_recurse(inDir, outZip, "", 0);
21  
}
22  
23  
static int dir2zip_recurse(File inDir, ZipOutputStream outZip, String outPrefix, int level) ctex {
24  
  if (++level >= 20) fail("woot? 20 levels in zip?");
25  
  
26  
  new L<File> files;
27  
  for (File f : listFiles(inDir))
28  
    files.add(f);
29  
30  
  int n = 0;
31  
  sortFilesByName(files);
32  
  for (File f : files) {
33  
    if (f.isDirectory()) {
34  
      if (dir2zip_recurse_verbose) print("dir2zip_recurse: Scanning " + f.getAbsolutePath());
35  
      n += dir2zip_recurse(f, outZip, outPrefix + f.getName() + "/", level);
36  
    } else {
37  
      if (dir2zip_recurse_verbose) print("Copying " + f.getName());
38  
      outZip.putNextEntry(new ZipEntry(outPrefix + f.getName()));
39  
      InputStream fin;
40  
      try {
41  
        fin = new FileInputStream(f);
42  
      } catch e {
43  
        print(e);
44  
        continue;
45  
      }
46  
      temp fin;
47  
      copyStream(fin, outZip);
48  
      ++n;
49  
    }
50  
  }
51  
  return n;
52  
}

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: 635 / 1071
Version history: 3 change(s)
Referenced in: [show references]