Package com.spring.mvc.zip;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Enumeration;
Import Java.util.zip.ZipEntry;
Import Org.apache.tools.zip.ZipFile;
public class Zip {
/**
* Unzip to the specified directory
* @param Zippath
* @param descdir
* @author isea533
*/
public static void Unzipfiles (String zippath,string descdir) throws exception{
Unzipfiles (New File (Zippath), descdir);
}
/**
* Extract files to the specified directory
* @param zipfile
* @param descdir
* @author isea533
*/
@SuppressWarnings ("Rawtypes")
public static void Unzipfiles (File zipfile,string descdir) throws exception{
File Pathfile = new file (Descdir);
if (!pathfile.exists ()) {
Pathfile.mkdirs ();
}
ZipFile zip = new ZipFile (ZipFile, "GBK");
For (Enumeration entries = Zip.getentries (); entries.hasmoreelements ();) {
ZipEntry entry = (zipentry) entries.nextelement ();
String zipentryname = Entry.getname ();
InputStream in = Zip.getinputstream ((org.apache.tools.zip.ZipEntry) entry);
String Outpath = (descdir+zipentryname). ReplaceAll ("\\*", "/");;
To determine if a path exists, create a file path if it does not exist
File File = new file (outpath.substring (0, Outpath.lastindexof ('/')));
if (!file.exists ()) {
File.mkdirs ();
}
Determine whether the full path of the file is a folder, if it is uploaded above, do not need to unzip
if (new File (Outpath). Isdirectory ()) {
Continue
}
Output file path information
System.out.println (Outpath);
OutputStream out = new FileOutputStream (Outpath);
byte[] buf1 = new byte[1024];
int Len;
while ((Len=in.read (BUF1)) >0) {
Out.write (Buf1,0,len);
}
In.close ();
Out.close ();
}
System.out.println ("****************** decompression completed ********************");
}
public static void Main (string[] args) throws Exception {
/**
* Extract Files
*/
File ZipFile = new file ("D://zhouzhiwei.zip"); Zip Package Address
String Path = "d://zipfile/"; Be extracted to this path
Unzipfiles (ZipFile, path);
}
}
Java Decompression zip archive package