Java Decompression ZIP archive package

Source: Internet
Author: User

Because the recent project needs to bulk upload files, and here the batch is to compress the file in a zip package, and then read the file to parse the contents of the file.

Therefore, the uploaded zip package needs to be decompressed first. The following direct code is provided for reference:

1. The first method is the method used to extract the ZIP compression package.

2. The second method is to delete the folder and the subdirectories and subdirectory files.

3. The third method is to delete all files and subdirectories in the folder.

Because we generally deal with parsing data after the need to delete the uploaded files to reduce the pressure on the server, so provide a second to third method for reference.

The way I extract the zip package here is to return the extracted file list, you can also return the desired results as needed, or do not return to the line.

If you have any better ways to welcome the message, please give us a lot of advice!

1  PackageCom.forms.oa.weekreport.batchimport.service;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.InputStream;7 Importjava.util.ArrayList;8 Importjava.util.Enumeration;9 Importjava.util.List;Ten  One ImportOrg.apache.tools.zip.ZipEntry; A ImportOrg.apache.tools.zip.ZipFile; -  -  Public classFileutil { the      -     /** - * Unzip the ZIP package and return to the list of files obtained after extracting - * <br> +      * @paramZippath -      * @return + * list<file> A      */ at      Public StaticList<file>unzipfile (String zippath) { -File File =NewFile (zippath); -         //set the folder with the same name as the compressed package in the directory where the compressed package is located. -String unzippath=zippath.substring (0, Zippath.lastindexof (".")));  -ZipFile ZipFile =NULL; -List<file> filelist=NewArraylist<file>(); in         Try { -             //set the encoding format toZipFile =NewZipFile (file, "GBK"); +}Catch(IOException E1) { - e1.printstacktrace (); the         } *Enumeration E =zipfile.getentries (); $          while(E.hasmoreelements ()) {Panax NotoginsengZipEntry ZipEntry =(ZipEntry) e.nextelement (); -             if(Zipentry.isdirectory ()) { theString name =zipentry.getname (); +Name = Name.substring (0,name.length ()-1);  AFile f =NewFile (Unzippath+file.separator +name);  the f.mkdirs (); +}Else {  -File f =NewFile (Unzippath +file.separator+zipentry.getname ());  $ Filelist.add (f); $ f.getparentfile (). Mkdirs (); -                 Try { - f.createnewfile (); theInputStream is =Zipfile.getinputstream (zipentry); -FileOutputStream fos =NewFileOutputStream (f);Wuyi                     intLength = 0;  the                     byte[] B =New byte[1024];  -                      while((Length=is.read (b, 0, 1024))!=-1) {  WuFos.write (b, 0, length);  -                     }  About is.close (); $ fos.close (); -}Catch(IOException E1) { - e1.printstacktrace (); -                 }  A             }  +         }  the         if(ZipFile! =NULL) {  -             Try { $ zipfile.close (); the}Catch(IOException E1) { the e1.printstacktrace (); the             }  the         }  -File.delete ();//Delete the compressed package when you are done extracting it in         returnFileList;//returns the list of files obtained after decompression the     } the      About     /** the * Delete this folder and subdirectories and subdirectory files <br> the      * @paramFolderPath void the      */ +      Public Static voidDelfolder (String folderpath) { -         Try { theDelallfile (FolderPath);//Delete all content insideBayiFile Path =NewFile (folderpath); thePath.delete ();//Delete Empty folders the}Catch(Exception e) { - e.printstacktrace (); -         } the     } the  the     /** the * Delete all files and subdirectories in the folder <br> -      * @paramPath the      * @returnBoolean the      */ the      Public Static Booleandelallfile (String path) {94         BooleanFlag =false; theFile File =NewFile (path); the         if(!file.exists ()) { the             returnFlag;98         } About         if(!file.isdirectory ()) { -             returnFlag;101         }102string[] Templist =file.list ();103File temp =NULL;104          for(inti = 0; i < templist.length; i++) { the             if(Path.endswith (file.separator)) {106temp =NewFile (path +templist[i]);107}Else {108temp =NewFile (path + file.separator +templist[i]);109             } the             if(Temp.isfile ()) {111 Temp.delete (); the             }113             if(Temp.isdirectory ()) { theDelallfile (path + file.separator + templist[i]);//Delete the files inside the folder first theDelfolder (path + file.separator + templist[i]);//then delete the empty folder theFlag =true;117             }118         }119         returnFlag; -     }121 122 123}

Java Decompression ZIP archive package

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.