No more nonsense to say, directly to everyone to paste Java code, the specific code as follows:
Java code
Package com.maidong.utils;
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;
Import Org.apache.http.protocol.HTTP; public class Ziputils {private static final int buff_size = 1024 * 1024;//1M Byte/** * Bulk Compressed files (clips) * * @param resfile List * File (folder) to be compressed * @param zipfile * Generated compressed file * @throws IOException * throw/public static void Zipfiles when the compression process goes wrong (collectio
N<file> resfilelist, File zipfile) throws IOException {Zipoutputstream zipout = null; try {zipout = new ZIPOUTPUTstream (New Bufferedoutputstream (New FileOutputStream (ZipFile), buff_size));
for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");
finally {if (zipout!= null) zipout.close (); /** * Bulk Compressed files (clips) * * @param resfilelist * List of files (folders) to compress * @param zipfile * Generated compressed file * @param Comment * Compressed file comments * @throw S IOException * throws/public static void Zipfiles (Collection<file> resfilelist, File zipfile, String commen When an error occurs in the compression process)
T) throws IOException {Zipoutputstream zipout = null;
try {zipout = new Zipoutputstream (new Bufferedoutputstream (New FileOutputStream (ZipFile), buff_size));
for (File resfile:resfilelist) {zipfile (resfile, ZipOut, "");
} zipout.setcomment (comment);
finally {if (zipout!= null) zipout.close (); /** * Extract a file * * @param zipfile * Compressed file * @param folderpath * Uncompressed target directory * @throws IOException * * When the decompression process error, throw * * Publ IC static void Upzipfile (file ZipFile, String folderpath) throws Zipexception, IOException {file Desdir = new file (FoldeRpath);
if (!desdir.exists ()) {desdir.mkdirs ();
ZipFile ZF = new ZipFile (ZipFile);
InputStream in = null;
OutputStream out = null; try {for (enumeration<?> entries = Zf.entries (); entries.hasmoreelements ();)
{ZipEntry entry = (zipentry) entries.nextelement ());
in = Zf.getinputstream (entry);
String str = folderpath + file.separator + entry.getname (); str = new String (str.getbytes ("8859_1"), HTTP.
UTF_8);
File Desfile = new file (str);
if (!desfile.exists ()) {File Fileparentdir = Desfile.getparentfile ();
if (!fileparentdir.exists ()) {fileparentdir.mkdirs ();
} desfile.createnewfile ();
out = new FileOutputStream (desfile);
byte buffer[] = new Byte[buff_size];
int reallength;
while ((Reallength = in.read (buffer)) > 0) {out.write (buffer, 0, reallength);
}}} finally {if (in!= null) in.close ();
if (out!= null) out.close (); /** * Extract file name containing incoming text * * @param zipfile * Compressed file * @param folderpath * target folder * @param namecontains * Incoming file matching name * @Throws Zipexception * is thrown * @throws IOException * IO error is thrown */public static arraylist<file> Upzipselectedfile ( File ZipFile, String folderpath, String namecontains) throws Zipexception, IOException {arraylist<file> filelist
= new Arraylist<file> ();
File Desdir = new file (FolderPath);
if (!desdir.exists ()) {Desdir.mkdir ();
ZipFile ZF = new ZipFile (ZipFile);
InputStream in = null;
OutputStream out = null; try {for (enumeration<?> entries = Zf.entries (); entries.hasmoreelements ();)
{ZipEntry entry = (zipentry) entries.nextelement ());
if (Entry.getname (). Contains (Namecontains)) {in = Zf.getinputstream (entry);
String str = folderpath + file.separator + entry.getname (); str = new String (str.getbytes ("8859_1"), HTTP.
UTF_8); Str.getbytes (Appconstans.utf_8), "8859_1" Output//Str.getbytes ("8859_1"), appconstans.utf_8 input File Desfile = new file (St
R);
if (!desfile.exists ()) {File Fileparentdir = Desfile.getparentfile (); if (!fileparentdir.Exists ()) {fileparentdir.mkdirs ();
} desfile.createnewfile ();
out = new FileOutputStream (desfile);
byte buffer[] = new Byte[buff_size];
int reallength;
while ((Reallength = in.read (buffer)) > 0) {out.write (buffer, 0, reallength);
} filelist.add (Desfile);
}}} finally {if (in!= null) in.close ();
if (out!= null) out.close ();
return filelist; /** * Obtain compressed file list * * @param zipfile * Compressed file * @return Compressed file name * @throws zipexception * Compressed file Format error when thrown * @throws Ioexcep tion * * public static arraylist<string> getentriesnames (File zipfile) throws zipexception when an error occurs in the decompression process Ioexceptio
n {arraylist<string> entrynames = new arraylist<string> ();
enumeration<?> entries = getentriesenumeration (ZipFile);
while (Entries.hasmoreelements ()) {ZipEntry entry = ((ZipEntry) entries.nextelement ()); Entrynames.add (New String (Getentryname (Entry). GetBytes (HTTP.
Utf_8), "8859_1");
return entrynames; /** * Gets the compressed file object for compressed files to obtain its properties * * @param zipfilE * Compressed file * @return Returns a list of compressed files * @throws zipexception * Compressed file with incorrect format throw * @throws IOException * IO operation error thrown/public static Enu Meration<?> getentriesenumeration (File zipfile) throws Zipexception, IOException {zipfile ZF = new ZipFile (zipFile
);
return Zf.entries (); /** * Get a comment on a compressed file object * * @param entry * Compressed file object * @return Compressed file object annotation * @throws unsupportedencodingexception/Public stat IC String getentrycomment (ZipEntry entry) throws Unsupportedencodingexception {return new String (Entry.getcomment (). GetBytes (HTTP.
Utf_8), "8859_1"); /** * Gets the name of the compressed file object * * @param entry * Compressed File object * @return The name of the compressed file object * @throws unsupportedencodingexception/Public stat IC String getentryname (ZipEntry entry) throws Unsupportedencodingexception {return new String (Entry.getname (). getBytes (HTTP.
Utf_8), "8859_1"); /** * Compressed File * * @param resfile * files (clips) that need to be compressed * @param zipout * Compressed destination file * @param rootpath * Compressed file path * @throws Filenotfoun Dexception * Throw * @throws IOException * When the file is not found * * When the compression process error 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"), HTTP.
UTF_8);
Bufferedinputstream in = null;
try {if (Resfile.isdirectory ()) {file[] filelist = Resfile.listfiles ();
for (file file:filelist) {zipfile (file, zipout, RootPath);
} else {byte buffer[] = new Byte[buff_size];
in = new Bufferedinputstream (new FileInputStream (resfile), buff_size);
Zipout.putnextentry (New ZipEntry (RootPath));
int reallength;
while ((Reallength = in.read (buffer))!=-1) {zipout.write (buffer, 0, reallength);
} in.close ();
Zipout.flush ();
Zipout.closeentry ();
Finally {if (in!= null) in.close ();
if (zipout!= null);
Zipout.close (); }
}
}
The end of the code, on the Android implementation of compression and decompression of the full content of the file to introduce so many people, I hope to help everyone!