Import java.util.zip.* in the Java API; The package contains all the related operations of Java for compressed files.
We can use the method in the package, combined with the relevant knowledge in Io, the file compression and decompression related operations.
ZipFile
Every compressed file in Java can be represented using ZipFile.
New file ("F:/zippath.zip"new zipfile (file); System.out.println ("The name of the compressed file is:" + zipfile.getname ());
Compress a single file
/**Compress a single file*/PublicStaticvoidZipFile (string filepath, string zippath) {Try{File File =NewFile (filepath); File ZipFile = new file (Zippath); InputStream input = new fileinputstream (file); Zipoutputstream zipout = new Zipoutputstream (new FileOutputStream (ZipFile)); Zipout.putnextentry (New ZipEntry (File.getname ())); int temp = 0; While (temp = Input.read ())! =-1) {zipout.write (temp);} input.close (); Zipout.close ();} Catch (Exception e) {e.printstacktrace ();}}
Application:
ZipFile ("D:/hello.txt", "D:/hello.zip");
Compress multiple files (folders)
/**Compress multiple files at once, file in one folder*/PublicStaticvoidZipmultifile (string filepath, string zippath) {Try{File File =New File (filepath);//folder to be compressed File ZipFile =NewFile (Zippath); InputStream input =Null; Zipoutputstream zipout =New Zipoutputstream (NewFileOutputStream (ZipFile));if(File.isdirectory ()) {file[] files = file.listfiles (); For (int i = 0; i < files.length; + +i) {input = new fileinputstream (Files[i]); Zipout.putnextentry ( C8>new ZipEntry (File.getname () + File.separator + Files[i].getname ())); int temp = 0; While ((temp = Input.read ())! =-1) {zipout.write (temp);} input.close ();}} Zipout.close (); } catch (Exception e) {e.printstacktrace ();}}
Application:
Zipmultifile ("F:/uu", "F:/zippath.zip");
Unzip a single file
/**Unzip (unzip a single file)*/PublicStaticvoidZipcontrafile (String Zippath, String outfilepath, string filename) {Try{File File =New File (Zippath);//Compressed file path and filename File OutFile =new File (Outfilepath); // decompression path and filename zipfile zipfile = new zipfile (file); ZipEntry entry = zipfile.getentry (filename); // extracted filename inputstream input = Zipfile.getinputstream (entry); OutputStream output = new fileoutputstream (outFile); int temp = 0; While (temp = Input.read ())! =-1) {output.write (temp);} input.close (); Output.close ();} Catch (Exception e) {e.printstacktrace ();}}
Application:
Zipcontrafile ("D:/hello.zip", "D:/eee.txt", "hello.txt");
Unzip multiple Files Zipinputstream class: When we need to extract multiple files, ZipEntry cannot be used.
If you want to manipulate more complex compressed files, we must use the Zipinputstream class.
/**Decompression (contains multiple files in a compressed file) can be used instead of the method above. * Zipinputstream class * When we need to extract multiple files, ZipEntry will not be able to use, * if you want to manipulate more complex compressed files, we must use the Zipinputstream class **/PublicStaticvoidZipcontramultifile (String Zippath, String outzippath) {Try{File File =NewFile (Zippath); File OutFile =Null; ZipFile ZipFile =NewZipFile (file); Zipinputstream Zipinput =New Zipinputstream (NewFileInputStream (file)); ZipEntry entry =Null; InputStream input =Null; OutputStream output =Null;while ((entry = Zipinput.getnextentry ())! =Null{System.out.println ("unzip" + entry.getname () + "file")); OutFile =New File (Outzippath + file.separator + entry.getname ()); if (! Outfile.getparentfile (). exists ()) {Outfile.getparentfile (). mkdir (); if (! Outfile.exists ()) {outfile.createnewfile ();} input = Zipfile.getinputstream (entry); output = new FileOutputStream (OutFile); int temp = 0; While (temp = Input.read ())! =-1) {output.write (temp);} input.close (); Output.close ();}} Catch (Exception e) {e.printstacktrace ();}}
Application:
Java backend Framework source SPRINGMVC mybatis Oracle mysql maven HTML5 Bootstrap full new technology
Zipcontramultifile ("F:/zippath.zip", "d:/"); Zipcontramultifile ("D:/hello.zip", "d:/");
Java architecture [Java Basics] Compress and decompress files using Java.util.zip package