Package com.lanyuan.assembly.util;
Import java.io.BufferedOutputStream;
Import java.io.File;
Import java.io.FileOutputStream;
Import java.io.IOException;
Import java.io.InputStream;
Import java.util.Enumeration;
Import org.apache.tools.zip.ZipEntry;
Import org.apache.tools.zip.ZipFile;
/**
* Unzip the zip file tool class
* @author Zhangyongbo
*
*/
public class Ziputil
{
private static final int buffer = 2048;
/**
* Unzip zip file
* @param path file directory
*/
public static void UnZip (String Path)
{
int Count =-1;
String Savepath = "";
File File = null;
InputStream is = null;
FileOutputStream fos = null;
Bufferedoutputstream BOS = null;
Savepath = path.substring (0, path.lastindexof (".")) + file.separator; Save the extracted files directory
New File (savepath). mkdir (); Create a Save directory
ZipFile ZipFile = null;
Try
{
ZipFile = new ZipFile (path, "gbk"); Solve Chinese garbled problem
enumeration<?> entries = zipfile.getentries ();
While (entries.hasmoreelements ())
{
byte buf[] = new byte[buffer];
ZipEntry entry = (zipentry) entries.nextelement ();
String filename = entry.getname ();
Boolean ismkdir = false;
If (filename.lastindexof ("/")! =-1) {//check If this file has a folder
Ismkdir = true;
}
filename = Savepath + filename;
If (entry.isdirectory ()) {//if the folder was created first
File = new File (filename);
File.mkdirs ();
Continue
}
File = new File (filename);
If (!file.exists ()) {//if the directory is created first
If (ismkdir) {
New File (filename.substring (0, filename.lastindexof ("/"))). mkdirs (); The catalog is created first
}
}
File.createnewfile (); Create a file
is = Zipfile.getinputstream (entry);
FOS = new FileOutputStream (file);
BOS = new Bufferedoutputstream (fos, buffer);
While ((count = Is.read (buf)) >-1)
{
Bos.write (buf, 0, count);
}
Bos.flush ();
Bos.close ();
Fos.close ();
Is.close ();
}
Zipfile.close ();
}catch (ioexception Ioe) {
Ioe.printstacktrace ();
}finally{
try{
If (bos! = Null) {
Bos.close ();
}
If (fos! = Null) {
Fos.close ();
}
If (is = Null) {
Is.close ();
}
If (zipfile! = Null) {
Zipfile.close ();
}
}catch (Exception E) {
E.printstacktrace ();
}
}
}
/*public static void main (string[] Args)
{
UnZip ("f:\\110000002.zip");
String f = "f:\\110000002";
File File = new file (f);
string[] test=file.list ();
For (int I=0;i<test.length;i++) {
System.out.println (test[i]);
}
System.out.println ("------------------");
String FileName = "";
file[] templist = File.listfiles ();
for (int i = 0; i < templist.length; i++) {
If (templist[i].isfile ()) {
System.out.println ("file:" +templist[i]);
FileName = Templist[i].getname ();
System.out.println ("filename:" +filename);
}
If (templist[i].isdirectory ()) {
System.out.println ("folder:" +templist[i]);
}
}
} */
}
Java uncompressed zip file Ziputil