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:
is correct default (and in case too) compressed archive (using deflate method)?
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:
so, correct in case not compressed archive @ all?zipoutputstream.setmethod(zipoutputstream.deflated); zipoutputstream.setlevel(zipoutputstream.stored);
- is there more convenient-obvious method creating not-compressed archives?
- if set method
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
Post a Comment