Java File compression and decompression (2)

Source: Internet
Author: User
Package com.cn; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. util. enumeration; import java.util.zip. zipentry; import java.util.zip. zipfile; import java.util.zip. zipoutputstream; // error Summary: // 1 about file. isfile () and file. isdirectory () memory deviation. // The error indicates that if directory must be file, file is not necessarily directory // correct: file and directory are two different tasks. only f One of. // The value 1 indicates that a path is compressed directly. // 2 does not mean that we execute a statement file F = new file ("F: \ x.txt"); // an x.txt file is generated on the primary hard disk. the following operations must be performed. // file F = new file ("F: \ x.txt"); // If (! F. exists () {// F. createnewfile (); //} // Where F. createnewfile () indicates that an empty file is created. // in most cases, file F = new file ("F: \ x.txt") is executed ") // use the input stream to reuse the output stream to operate the stream. For example, use the following example: // write Hello World /// 3 to the x.txt file: // the ZOS stream is not disabled in the ZIP () method. an error occurred while extracting the compressed file. // Important Note: // 1. The core operation object for zip and unzip is every file !!! // For example, if you encounter a directory, it will traverse every file and compress it one by one. // do not mistakenly understand that a directory is compressed as a whole. // 2 in Java, each compressed file is represented by a zipentry. // You must obtain the complete path of each file during the compression process (from the outermost folder to the file itself) // use this full path to create a zipentry for each compressed file new () // 3 so zipentry can be used to record the original directory hierarchy. decompress the package to keep it as it is. // you can also see that entrys is used during decompression. hasmoreelements (). // extract each zipentry. // see the decompressed code: // new file (unzippath + file. separator + entry. getname (); public class testzipandunzip {public static void main (stri NG [] ARGs) throws exception {testzipandunzip test = new testzipandunzip (); // compress a file into zip test.zip ("F :\\", "KK \ cc.txt", "F: \ cc1.zip"); // extract a zip file to test. unzipfile ("f :\\ cc1.zip", "F :\\ ZZZZ");}/*** @ Param willzipdirpath: compressed file (directory) directory * @ Param willzipfilename name of the compressed file (directory) * @ Param tofilepath name of the compressed file (directory) */Public void zip (string willzipdirpath, string willzipfilename, string zipedfilename) {system. O Ut. println ("..................... The following is the ZIP () method .............................. "); If (willzipdirpath = NULL) {return;} file willzipdir = new file (willzipdirpath); If (! Willzipdir. exists () |! Willzipdir. isdirectory () {return;} // obtain the absolute directory path string willzipdirabsolutepath = willzipdir. getabsolutepath (); system. out. println ("willzipdir. getabsolutepath () = "+ willzipdirabsolutepath); // compressed file zipedfile = new file (zipedfilename ); try {// obtain the compressed output stream from the compressed file zipoutputstream // here ZOS only points to the outermost layer of the compressed file. so how does it // maintain the original directory structure? // Zipentry is used !!! // You can see the use of zipentry in the filetozip () method !! Zipoutputstream ZOS = new zipoutputstream (New fileoutputstream (zipedfile); If (willzipfilename. equals ("*") {// If the input is *, it indicates that everything in this path must be compressed. // call dirtozip () dirtozip (willzipdirabsolutepath, willzipdir, ZOS);} else {// file willzipfile = new file (willzipdirpath, willzipfilename) to be compressed; If (willzipfile. isfile () {system. out. println ("..................... The outermost layer starts to compress files ........................... "); Filetozip (willzipdirpath, willzipfile, ZOS); system. Out. println ("..................... End of the outermost compressed file ........................... ");} If (willzipfile. isdirectory () {system. Out. println ("..................... The outermost layer begins to compress the directory ........................... "); Dirtozip (willzipdirpath, willzipfile, ZOS); system. Out. println ("..................... The end of the outermost compressed directory ........................... ");} // Close the stream !!! ZOS. Close (); system. Out. println ("..................... The above is the ZIP () method .............................. ") ;}} Catch (exception e) {// todo: handle exception}/*** @ Param dirpath directory of the compressed file * @ Param willzipfile name of the compressed file * @ Param ZOS output stream */Public void filetozip (string dirpath, file willzipfile, zipoutputstream ZOS) {fileinputstream FCM = NULL; zipentry = NULL; byte [] buffer = new byte [1024*8]; int Len = 0; If (willzipfile. isfile () {try {FS = new fileinputstream (willzipfile); zipentry = new zipentry (Gete Ntryname (dirpath, willzipfile); ZOS. putnextentry (zipentry); system. Out. println ("..................... The following is the filetozip () method .............................. "); System. out. println ("zipentry. getname = "+ zipentry. getname (); system. out. println ("zipentry. isdirectory = "+ zipentry. isdirectory (); system. out. println ("zipentry. getsize = "+ zipentry. getsize (); system. out. println ("zipentry. gettime = "+ zipentry. gettime (); system. out. println ("zipentry. getcomment = "+ zipentry. getcomment (); system. out. println ("..................... The above is the filetozip () method .............................. "); While (LEN = Fi. Read (buffer ))! =-1) {ZOS. write (buffer, 0, Len);} ZOS. closeentry (); FCM. close ();} catch (exception E) {}}/ *** @ Param dirpath the parent directory where the directory to be compressed is located * @ Param willzipdir compressed directory * @ Param ZOS output stream */Public void dirtozip (string dirpath, file willzipdir, zipoutputstream ZOS) {If (willzipdir. isdirectory () {file [] files = willzipdir. listfiles (); // process --> no file in this folder if (files. length = 0) {zipentry = new zipentry (getentryname (dirpath, Willzipdir); system. out. println ("xxxxxxxxxxxxxxxx" + zipentry. getname (); try {ZOS. putnextentry (zipentry); // ZOS. closeentry ();} catch (exception e) {e. printstacktrace ();} return;} // process --> all files in this folder for (INT I = 0; I <files. length; I ++) {file = files [I]; // recursively call filetozip () if (file. isfile () {system. out. println ("xxxxxxxxxx starts the filetozip () method xxxxxxxxxx"); filetozip (dirpath, file, ZOS); system. out. Println ("xxxxxxxxxx internal filetozip () method ends xxxxxxxxxx");} // if it is a file, recursively call dirtozip () if (file. isdirectory () {system. out. println ("xxxxxxxxxx"); dirtozip (dirpath, file, ZOS); system. out. println ("xxxxxxxxxx inner dirtozip () method ends xxxxxxxxxx ");}}}} /*** @ Param dirpath: directory where the compressed file is located * @ Param willzipfile: the compressed file * @ return * // generates the complete path of each file (from the outermost layer folder to the file itself) // The generated zipentry records the original directory hierarchy. decompress the package to make it public string G. Etentryname (string dirpath, file willzipfile) {If (! Dirpath. endswith (file. separator) {dirpath + = file. separator;} string willzipfilepath = willzipfile. getabsolutepath (); If (willzipfile. isdirectory () {willzipfilepath + = "/";} int Index = willzipfilepath. indexof (dirpath); system. out. println ("XX returned entryname =" + willzipfilepath. substring (index + dirpath. length (); Return willzipfilepath. substring (index + dirpath. length ();}/*** @ Param zipedfilename: zip file to be decompressed * @ Pa The outermost file name * @ throws ioexception */Public void unzipfile (string zipedfilename, string unzipdirpath) throws exception {If (! Unzipdirpath. endswith (file. separator) {unzipdirpath + = file. separator;} Try {zipfile zipedfile = new zipfile (zipedfilename); zipentry = NULL; string entryname = NULL; string unzipedfilename = NULL; enumeration entrys = zipedfile. entries (); byte [] buffer = new byte [1024*8]; int Len = 0; while (entrys. hasmoreelements () {zipentry = (zipentry) entrys. nextelement (); entryname = zipentry. getname (); unzipedfilename = unzipdi Rpath + entryname; system. Out. println ("..................... The following is the unzipfile () method .............................. "); System. out. println ("zipedfilename =" + zipedfilename); system. out. println ("unzipdirpath =" + unzipdirpath); system. out. println ("entryname =" + entryname); system. out. println ("unzipedfilename =" + unzipedfilename); system. out. println ("..................... The above is the unzipfile () method .............................. "); If (zipentry. isdirectory () {// The code system. out. println ("999999999999"); new file (unzipedfilename ). mkdirs ();} else {// always execute this code. because each file is compressed during compression. new file (unzipedfilename ). getparentfile (). mkdirs ();} fileoutputstream Fos = NULL; inputstream is = NULL; file unzipedfile = new file (unzipedfilename); If (unzipedfile. isdirectory () {file [] files = unzipedfile. listfiles (); For (INT I = 0; I <files. length; I ++) {f Ile file = files [I]; Fos = new fileoutputstream (File); is = zipedfile. getinputstream (zipentry); While (LEN = is. Read (buffer ))! =-1) {FOS. write (buffer, 0, Len) ;}} else {Fos = new fileoutputstream (unzipedfile); is = zipedfile. getinputstream (zipentry); While (LEN = is. read (buffer ))! =-1) {FOS. write (buffer, 0, Len) ;}// here you need to modify // FOS. close (); // is. close () ;}} catch (exception e) {e. printstacktrace ();}}}

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.