Java programming notes 18 File compression and decompression

Source: Internet
Author: User

In Java, zipentry, zipinputstream, and zipoutputstream are mainly used to implement the programming method of zip data compression,

Constructor Summary
ZipEntry(String name)

Create a new ZIP entry with the specified name.
Constructor Summary
ZipInputStream(InputStream in)

Create a new zip input stream.
Constructor Summary
ZipOutputStream(OutputStream out)

Create a new zip output stream.

Main zipinputstream Method

ZipEntry getNextEntry()

Read the next zip file entry and locate the stream at the beginning of the entry data.
 void closeEntry()

Close the current ZIP entry and locate the stream to read the next entry.

Compression Test Program:

Package COM. book. ch09.file; import Java. io. file; import Java. io. filefilter; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; public class compressfile {public static void AddFile (zipoutputstream zipoutput, file) throws ioexception {system. out. println ("adding file:" + file. getabsolutepath (); // each The compressed object is a zipentry instance. Zipentry = new zipentry (file. getname (); // put this instance into the compressed file zipoutput. putnextentry (zipentry); // read the file content fileinputstream ins = new fileinputstream (File); byte [] TMP = new byte [1024]; int Len = 0; while (LEN = ins. read (TMP ))! =-1) {zipoutput. write (TMP, 0, Len);} ins. close (); // close the current zipentry instance zipoutput. closeentry ();} public static void main (string [] ARGs) throws ioexception {file sourcefolder = new file ("D: \ common software "); file targetfile = new file ("D: \ zip_test.zip"); // use zipoutputstream to create a compressed file zipoutputstream zipoutput = new zipoutputstream (New fileoutputstream (targetfile )); // list all files in this folder. File [] subfiles = sourcefolder. listfiles (New filefilter () {public Boolean accept (File pathname) {If (pathname. isfile () return true; return false ;}}); system. out. println ("\ r \ n File compression start:" + sourcefolder. getabsolutepath () + "\ n"); For (INT I = 0; I <subfiles. length; I ++) {AddFile (zipoutput, subfiles [I]);} zipoutput. close (); system. out. println ("\ r \ n file compressed:" + targetfile. getabsolutepath ());}}

Running result:

File compression starts: D: \ common software

Adding file: D: \ common software \ apache_2.2.4-win32-x86-no_ssl.zip
Adding file: D: \ common software \ cajviewer7.0.zip
Adding file: D: \ common software \ ccleaner.rar
Adding file: D: \ common software \ ctex_2.8.0.125.exe
Adding file: D: \ common software \ Download
Adding file: D: \ common software \ duba2008_down_31_00006.exe
Adding file: D: \ common software \ dzh_internet_v560w.exe
Adding file: D: \ common software \ EasyRecoveryPro-v6.20.rar
Adding file: D: \ common software \ FinalData-v2.01.rar
Adding file: D: \ common software \ firefoxchinaedition 2010.12.exe
Adding file: D: \ common software \ FirefoxChinaEdition-latest.exe
Adding file: D: \ common software \ fxalendar.exe
............

............

File compression completed: D: \ zip_test.zip

Decompress:

Package COM. book. ch09.file; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import java.util.zip. zipentry; import java.util.zip. zipinputstream; public class uncompressfile {static int COUNT = 0; public static void extractfile (file path, zipinputstream zipins, zipentry) throws ioexception {file = new file (path, zipentry. getname () ); System. out. println ("releasing file:" + file. getabsolutepath (); fileoutputstream ous = new fileoutputstream (File); byte [] TMP = new byte [1024]; int Len = 0; while (LEN = zipins. read (TMP ))! =-1) {ous. write (TMP, 0, Len);} ous. close (); zipins. closeentry (); count ++;} public static void main (string [] ARGs) throws ioexception {file sourcezipfile = new file ("C: \ zip_test.zip "); file targetfolder = new file ("C: \ zip_test"); targetfolder. mkdir (); zipinputstream zipins = new zipinputstream (New fileinputstream (sourcezipfile); zipentry = zipins. getnextentry (); While (zipentry! = NULL) {extractfile (targetfolder, zipins, zipentry); zipentry = zipins. getnextentry ();} zipins. close (); system. out. println ("\ r \ n file released successfully. "+ Count +" files are released. ");}}
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.