Java uncompressed zip file Ziputil

Source: Internet
Author: User

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

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.