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