java - Compression of zip archive -


this question has answer here:

i'm trying compress directory content zip archive using java. fine, want clarify facts. here code use compress files:

public void pack(@nonnull string archivename, @nonnull file outputdir, @nonnull file targetdir) {   file zipfile = new file(outputdir, "out.zip");    zipoutputstream zipoutputstream = null;   outputstream outputstream;   try {     // create stream writing zip archive     outputstream = new fileoutputstream(zipfile);     zipoutputstream = new zipoutputstream(outputstream);     // write files recursively     writefiles(zipoutputstream, targetdir.listfiles(), "");   } catch (ioexception e) {     logger.error("io exception while packing files archive", e);   } {     // close output streams     if (zipoutputstream != null) {       try {         zipoutputstream.close();       } catch (ioexception e) {         logger.error("unable close zip output stream", e);       }     }   } }  /**  * writes specified files , children (in case of directories) archive  *  * @param zipoutputstream archive output stream  * @param files           should added archive  * @param path            path relative of root of archive files should placed  */ private void writefiles(@nonnull zipoutputstream zipoutputstream, @nullable file[] files, @nonnull string path) throws ioexception {   if (files == null || files.length == 0) {     return;   }    (file file : files) {     if (file.isdirectory()) {       // recursively add files in directory       string fulldirectoryname = path + file.getname() + "/";       file[] childfiles = file.listfiles();       if (childfiles != null && childfiles.length > 0) {         // write child files archive. current directory created automatically         writefiles(zipoutputstream, childfiles, fulldirectoryname);       } else {         // empty directory. write directory archive         zipentry entry = new zipentry(fulldirectoryname);         zipoutputstream.putnextentry(entry);         zipoutputstream.closeentry();       }     } else {       // put file in archive       bufferedinputstream bufferedinputstream = new bufferedinputstream(new fileinputstream(file));       zipoutputstream.putnextentry(new zipentry(path + file.getname()));       bytestreams.copy(bufferedinputstream, zipoutputstream);       zipoutputstream.closeentry();       bufferedinputstream.close();     }   } }

now there questions:

  1. is correct default (and in case too) compressed archive (using deflate method)?

  2. how uncompressed archive:

    • if set method zipoutputstream.setmethod(zipoutputstream.stored) have provide size, compressed size (is equal size?) , crc, otherwise exceptions
    • if don't want calculate size , crc myself can use deflate method 0 level:
      zipoutputstream.setmethod(zipoutputstream.deflated); zipoutputstream.setlevel(zipoutputstream.stored);
      so, correct in case not compressed archive @ all?
    • is there more convenient-obvious method creating not-compressed archives?

thank attention. have day!

rather re-invent wheel i'd consider using existing library this, such apache ant. basic idiom creating zip file is:

project p = new project(); p.init(); zip zip = new zip(); zip.setproject(p); zip.setdestfile(new file(outputdir, "out.zip")); fileset fs = new fileset(); fs.setproject(p); fs.setdirectory(targetdir); zip.addfileset(fs); zip.perform(); 

by default compressed archive. uncompressed zip need add is

zip.setcompress(false); 

after setdestfile (in fact anywhere before perform).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -