Android Zip compression

Source: Internet
Author: User

Recently need to compress the output files, understand the next Android compression function, the source code is as follows:

Activity invocation:

Try {            ziputil2.zip (Exportpath, Exportpath+ ". zip");         Catch (Exception e) {            //  TODO auto-generated catch block            e.printstacktrace ();        } 

Compression class Prototypes:

 Packagecom.tecsun.idc.utils;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Enumeration;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile;ImportJava.util.zip.ZipOutputStream; Public classZIPUTIL2 { Public Static voidZip (string src, string dest)throwsIOException {//provides a data item to compress into a zip archive output streamZipoutputstream out =NULL; Try{File OutFile=NewFile (dest);//source files or directoriesFile fileordirectory =NewFile (SRC);//Compressed file pathout =NewZipoutputstream (NewFileOutputStream (outFile)); //False If this file is a file.        if(Fileordirectory.isfile ()) {zipfileordirectory (out, Fileordirectory,""); } Else {        //returns a file or an empty array. file[] Entries =Fileordirectory.listfiles ();  for(inti = 0; i < entries.length; i++) {         //recursive compression, update curpathsZipfileordirectory (out, entries[i], ""); }       }      } Catch(IOException ex) {ex.printstacktrace (); } finally {       //turn off the output stream       if(Out! =NULL) {        Try{out.close (); } Catch(IOException ex) {ex.printstacktrace (); }       }      }     }          Private Static voidZipfileordirectory (Zipoutputstream out, File fileordirectory, String curpath)throwsIOException {//reads the input stream of bytes from a fileFileInputStream in =NULL; Try {       //returns False if this file is a directory.        if(!fileordirectory.isdirectory ()) {        //Compress Files        byte[] buffer =New byte[4096]; intBytes_read; Inch=NewFileInputStream (fileordirectory); //instance represents a zip archive within a single entryZipEntry entry =NewZipEntry (Curpath+fileordirectory.getname ()); //entry information is written to the underlying streamout.putnextentry (entry);  while((Bytes_read = in.read (buffer))! =-1) {out.write (buffer,0, Bytes_read);       } out.closeentry (); } Else {        //Compressed Directoryfile[] Entries =Fileordirectory.listfiles ();  for(inti = 0; i < entries.length; i++) {         //recursive compression, update curpathszipfileordirectory (out, entries[i], Curpath+ fileordirectory.getname () + "/"); }       }      } Catch(IOException ex) {ex.printstacktrace (); //throw ex;}finally {       if(In! =NULL) {        Try{in.close (); } Catch(IOException ex) {ex.printstacktrace (); }}} @SuppressWarnings ("Unchecked")      Public Static voidUnzip (string zipfilename, string outputdirectory)throwsIOException {zipfile zipfile=NULL; Try{ZipFile=NewZipFile (zipfilename); Enumeration e=zipfile.entries (); ZipEntry ZipEntry=NULL; File dest=NewFile (outputdirectory);       Dest.mkdirs ();  while(E.hasmoreelements ()) {ZipEntry=(ZipEntry) e.nextelement (); String EntryName=Zipentry.getname (); InputStream in=NULL; FileOutputStream out=NULL; Try {         if(Zipentry.isdirectory ()) {String name=Zipentry.getname (); Name= name.substring (0, Name.length ()-1); File F=NewFile (OutputDirectory +File.separator+name);         F.mkdirs (); } Else {          intindex = entryname.lastindexof ("\ \"); if(Index! =-1) {File DF=NewFile (OutputDirectory +File.separator+ entryname.substring (0, index));          Df.mkdirs (); } Index= Entryname.lastindexof ("/"); if(Index! =-1) {File DF=NewFile (OutputDirectory +File.separator+ entryname.substring (0, index));          Df.mkdirs (); } File F=NewFile (OutputDirectory +File.separator+zipentry.getname ()); //f.createnewfile ();in =Zipfile.getinputstream (ZipEntry); out=NewFileOutputStream (f); intC; byte[] by =New byte[1024];  while((c = In.read (by))! =-1) {Out.write (by,0, c);         } out.flush (); }        } Catch(IOException ex) {ex.printstacktrace (); Throw NewIOException ("Decompression failed:" +ex.tostring ()); } finally {         if(In! =NULL) {          Try{in.close (); } Catch(IOException ex) {}}if(Out! =NULL) {          Try{out.close (); } Catch(IOException ex) {}}} }      } Catch(IOException ex) {ex.printstacktrace (); Throw NewIOException ("Decompression failed:" +ex.tostring ()); } finally {       if(ZipFile! =NULL) {        Try{zipfile.close (); } Catch(IOException ex) {}}} }    }

Android Zip compression

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.