Java architecture [Java Basics] Compress and decompress files using Java.util.zip package

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.