Zip decompression in Android

Source: Internet
Author: User

Zip decompression in Android

Import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. collection; import java. util. enumeration; import java.util.zip. zipEntry; import java.util.zip. zipException; import java.util.zip. zipFile; import java.util.zip. zipOutputStream;/*** Android Zip compression and decompression */public class Unzip {private static final int BUFF_SIZE = 1024*1024; // 1 M Byte/*** batch compressed file (folder) ** @ param resFileList file to be compressed (folder) list * @ param zipFile: the compressed file generated * @ throws IOException thrown when the compression process fails */public static void zipFiles (Collection
 
  
ResFileList, File zipFile) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile), BUFF_SIZE); for (File resFile: resFileList) {zipFile, zipout, "");} zipout. close ();}/*** batch compressed file (folder) ** @ param resFileList file to be compressed (folder) list * @ param zipFile generated compressed file * @ param comment comments of compressed file * @ throws IOException thrown when the compression process fails */public static void zipFiles (Collection
  
   
ResFileList, File zipFile, String comment) throws IOException {ZipOutputStream zipout = new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (zipFile), BUFF_SIZE); for (File resFile: resFileList) {zipFile (resFile, zipout, "");} zipout. setComment (comment); zipout. close ();}/*** decompress a file ** @ param zipFile compressed file * @ param folderPath decompressed target directory * @ param isDelete * @ throws IOException when resolving When an error occurs during the compression process, */public static void upZipFile (File zipFile, String folderPath, boolean isDelete) {String strZipName = zipFile is thrown. getName (); folderPath + = "/" + strZipName. substring (0, strZipName. lastIndexOf (". "); File desDir = new File (folderPath); if (! DesDir. exists () {desDir. mkdirs ();} ZipFile zf; try {zf = new ZipFile (zipFile); for (Enumeration
   Entries = zf. entries (); entries. hasMoreElements ();) {ZipEntry entry = (ZipEntry) entries. nextElement (); if (entry. isDirectory () {String dirstr = entry. getName (); dirstr = new String (dirstr. getBytes ("8859_1"), "GB2312"); File f = new File (dirstr); f. mkdir (); continue;} InputStream in = zf. getInputStream (entry); String str = folderPath + File. separator + entry. getName (); str = new String (str. getByt Es ("8859_1"), "GB2312"); File desFile = new File (str); if (! DesFile. exists () {File fileParentDir = desFile. getParentFile (); if (! FileParentDir. exists () {fileParentDir. mkdirs ();} desFile. createNewFile ();} OutputStream out = new FileOutputStream (desFile); byte buffer [] = new byte [BUFF_SIZE]; int realLength; while (realLength = in. read (buffer)> 0) {out. write (buffer, 0, realLength);} in. close (); out. close () ;}if (isDelete) {zipFile. delete () ;}} catch (ZipException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}} /*** extract the file name containing the input text ** @ param zipFile compressed file * @ param folderPath destination folder * @ param nameContains imported file matching name * @ throws ZipException compression format * @ throws IOException IO thrown by mistake */public static ArrayList
   
    
UpZipSelectedFile (File zipFile, String folderPath, String nameContains) throws ZipException, IOException {ArrayList
    
     
FileList = new ArrayList
     
      
(); File desDir = new File (folderPath); if (! DesDir. exists () {desDir. mkdir ();} ZipFile zf = new ZipFile (zipFile); for (Enumeration
      Entries = zf. entries (); entries. hasMoreElements ();) {ZipEntry entry = (ZipEntry) entries. nextElement (); if (entry. getName (). contains (nameContains) {InputStream in = zf. getInputStream (entry); String str = folderPath + File. separator + entry. getName (); str = new String (str. getBytes ("8859_1"), "GB2312"); // str. getBytes ("GB2312"), "8859_1" output // str. getBytes ("8859_1"), "GB2312", input File desFile = new File (str); if (! DesFile. exists () {File fileParentDir = desFile. getParentFile (); if (! FileParentDir. exists () {fileParentDir. mkdirs ();} desFile. createNewFile ();} OutputStream out = new FileOutputStream (desFile); byte buffer [] = new byte [BUFF_SIZE]; int realLength; while (realLength = in. read (buffer)> 0) {out. write (buffer, 0, realLength);} in. close (); out. close (); fileList. add (desFile) ;}} return fileList ;} /*** get the file list in the compressed file ** @ param zipFile ZIP file * @ return the file name in the compressed file * @ throws ZipException: if the format of the compressed file is incorrect, throw * @ throws IOException when when an error occurs during decompression */public static ArrayList
      
        GetEntriesNames (File zipFile) throws ZipException, IOException {ArrayList
       
         EntryNames = new ArrayList
        
          (); Enumeration
         Entries = getEntriesEnumeration (zipFile); while (entries. hasMoreElements () {ZipEntry entry = (ZipEntry) entries. nextElement (); entryNames. add (new String (getEntryName (entry ). getBytes ("GB2312"), "8859_1");} return entryNames ;} /*** obtain the compressed file object in the compressed file to obtain its attributes ** @ param zipFile compressed file * @ return returns a compressed file list * @ throws ZipException thrown when the compressed file format is incorrect * @ throws IOException when the IO operation is incorrect */public static Enumeration
         GetEntriesEnumeration (File zipFile) throws ZipException, IOException {ZipFile zf = new ZipFile (zipFile); return zf. entries ();} /*** get the annotation of the compressed file object ** @ param entry compresses the file object * @ return the annotation of the compressed file object * @ throws UnsupportedEncodingException */public static String getEntryComment (ZipEntry entry) throws UnsupportedEncodingException {return new String (entry. getComment (). getBytes ("GB2312"), "8859_1");}/*** get Name of the compressed file object ** @ param entry: name of the compressed file object * @ return: name of the compressed file object * @ throws UnsupportedEncodingException */public static String getEntryName (ZipEntry entry) throws UnsupportedEncodingException {return new String (entry. getName (). getBytes ("GB2312"), "8859_1");}/*** compressed file ** @ param resFile the file to be compressed (folder) * @ param zipout: the target file to be compressed * @ param rootpath: the compressed file path * @ throws FileNotFoundException throws * @ throws IOExce Ption throws */private static void zipFile (File resFile, ZipOutputStream zipout, String rootpath) throws FileNotFoundException, IOException {rootpath = rootpath + (rootpath. trim (). length () = 0? "": File. separator) + resFile. getName (); rootpath = new String (rootpath. getBytes ("8859_1"), "GB2312"); if (resFile. isDirectory () {File [] fileList = resFile. listFiles (); for (File file: fileList) {zipFile (file, zipout, rootpath) ;}} else {byte buffer [] = new byte [BUFF_SIZE]; bufferedInputStream in = new BufferedInputStream (new FileInputStream (resFile), BUFF_SIZE); zipout. putNextEntry (new Zip Entry (rootpath); int realLength; while (realLength = in. read (buffer ))! =-1) {zipout. write (buffer, 0, realLength);} in. close (); zipout. flush (); zipout. closeEntry ();}}}
        
       
      
     
    
   
  
 

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.