Online streaming zip compression and decompression This code has a very big problem despite using ant compression and decompression. But the task flow or the way to write using Java.util.zip, the process that I used in the compressed folder structure encountered is not correct, even if the different file decompression software shows the different predicament of the folder structure.
Use the following to give the Org.apache.tools.ant.taskdefs.Zip, and the Org.apache.tools.ant.taskdefs.Expand compression and decompression process.
Import Java.io.file;import Org.apache.tools.ant.buildexception;import Org.apache.tools.ant.project;import Org.apache.tools.ant.taskdefs.expand;import Org.apache.tools.ant.taskdefs.zip;import Org.apache.tools.ant.types.fileset;public class Zipper {public final static String encoding = "GBK";//Compressed public static Voi D Zip (String srcpathname, String zipfilepath) throws Buildexception, runtimeexception {File file = new file (srcpathname); F (!file.exists ()) throw new RuntimeException ("source file or directory" + Srcpathname + "does not exist."); Project proj = new Project (); Fileset fileset = new Fileset () Fileset.setproject (proj);//inference is a folder or a file if (File.isdirectory ()) {fileset.setdir (file); /ant include/exclude rules can be used here//For example://Fileset.setexcludes ("**/*.txt");//Fileset.setincludes ("**/*.xls");} else {fileset.setfile (file);} Zip zip = new zip (); Zip.setproject (proj); Zip.setdestfile (new File (Zipfilepath)); Zip.addfileset (Fileset); zip.setencoding (encoding); Zip.execute ();} Uncompress public static void unzip(String Zipfilepath, String destDir) throws Buildexception, RuntimeException {if (!new File (Zipfilepath). Exists ()) throw New RuntimeException ("Zip file" + zipfilepath+ "does not exist."); Project proj = new Project (); Expand Expand = new Expand () Expand.setproject (proj); Expand.settasktype ("unzip"); Expand.settaskname ("unzip"); expand.setencoding (encoding); expand.setsrc (new File (Zipfilepath)); expand.setdest (new File (DestDir)); Expand.execute ();} public static void Main (String []args) {unzip ("D:\\123.zip", "d:\\123"), Zip ("d:\\upload", "D:\\upload.zip");}}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Correct zip compression and decompression code