[Java Basics] use the java.util.zip package to compress and decompress files

Source: Internet
Author: User

[Java Basics] use the java.util.zip package to compress and decompress files
The import java.util.zip. * package in Java API contains all Java operations on compressed files. We can use the methods in this package to perform File compression and decompression operations based on the relevant knowledge in IO. Each compressed file in ZipFilejava can be represented by ZipFile.

File file = new File ("F:/zippath.zip"); ZipFile zipFile = new ZipFile (file); System. out. println ("compressed file name:" + zipFile. getName (); compress a single File/** compress a single file */public static void ZipFile (String filepath, String zippath) {try {File = new file (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)/** one-time compress multiple files, files are stored in a folder */public static void ZipMultiFile (String filepath, String zippath) {try {File file = new File (filepath ); // File zipFile = new File (zippath); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream (new FileOutputStream (zipFile); if (file. isDirectory () {File [] files = file. listFiles (); for (int I = 0 ; I <files. length; ++ I) {input = new FileInputStream (files [I]); zipOut. putNextEntry (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"); decompress a single file/** (decompress a single file) */public static void ZipContraFile (String zippath, String outfilepath, String filename) {try {File file = new File (zippath ); // compressed File path and File name outFile = new file (outfilepath); // decompress the File path and File name ZipFile zipFile = new ZipFile (file); ZipEntry entry = zipFile. getEntry (filename); // The extracted file name is InputStream input = zipFile. getInputStream (entry); Outpu TStream 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"); decompress multiple files in the ZipInputStream class: When we need to decompress multiple files, zipEntry cannot be used. To operate more complex compressed files, we must use the ZipInputStream class. /** Extract (the compressed file contains multiple files) instead of the preceding method. * ZipInputStream class * ZipEntry cannot be used when multiple files need to be decompressed. * to perform operations on more complex compressed files, we must use the ZipInputStream class **/public static void ZipContraMultiFile (String zippath, String outzippath) {try {File file = new File (zippath); File outFile = null; zipFile zipFile = new ZipFile (file); ZipInputStream zipInput = new ZipInputStream (new FileInputStream (file); ZipEntry entry = null; InputStream input = null; OutputStream Output = null; while (entry = zipInput. getNextEntry ())! = Null) {System. out. println ("extract" + 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: ZipContraMultiFile ("f:/zippath.zip", "d:/"); ZipContraMultiFile ("d:/hello.zip", "d :/");

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.