Atitit. Implementing best practices for extracting Zip files Java C #. NET PHP

Source: Internet
Author: User
Tags create directory
Atitit. Implementing best practices for extracting Zip files Java C #. NET PHP

1. JDK zip with Apache ant zip 1

2. Apache ant package for zip file compression, upzip approximate process. 1

3. Read the file name OK, but cant read to input said Npe. 2

4. Ant1.8.2.jar 2

5. #---Details Code 2

6. Reference 4

1. JDK zip with Apache ant zip

The following function is to extract the image files in the zip file into the current directory, with the JDK's own code processing ZIP file processing, but not the Chinese name of the file, otherwise it will be wrong.

The following is the Apache ZIP file processing package for processing, you can handle the Chinese name of the file, the function is the same as above.

Use Apache Ant version1.7 's tools.zip to extract zip files to solve Chinese problems

1.7 Blow still does not support Chinese.

Author:: Old Wow's paw Attilax Ayron, email:1466519819@qq.com

2. Apache ant package for zip file compression, upzip approximate process.

Many years ago encountered this kind of business, unzip the ZIP standard compressed file. The previously written operation class is now missing, and this business is being processed in the recent project, so a new one has been written. Java provides an API for handling zip packages. But the Chinese support is not very good, so I directly with Apache ant in the zip operation API for processing. Ant's API solves the problem of Chinese support and is very convenient to use. The following is the action class.

The following classes only use a small subset of Apache functionality. For more specific APIs, please refer to the documentation. There's not much to explain here.

* Import Apache Ant.jar package into Lib in project

3. Read the file name OK, but cant read to input said Npe.

Cause:::encode problem: The default seems to be UTF8. But actually is GBK ...

Org.apache.tools.zip.ZipFile zipfile =new ZipFile (zipfilename, "GBK");

Second, the direct use of the Unzipfiles method ZipFile zip = new ZipFile (ZipFile); Decompression found that Chinese is still garbled, changed to zipfile zip = new ZipFile (ZipFile, "GBK"); After the Chinese is normal, and the project may be specific configuration and operating environment.

4. Ant1.8.2.jar

5. #---detailed code

/**

* Decompression static method

* @param zipfilename

* @param outputdirectory

* @throws Exception

*/

public static void Extract (String zipfilename,string outputdirectory,string encode) throws exception{

try {

= "Utf-8";

Org.apache.tools.zip.ZipFile zipfile =new ZipFile (zipfilename, encode);

New Org.apache.tools.zip.ZipFile (Zipfilename);

Java.util.Enumeration e = zipfile.getentries ();

Org.apache.tools.zip.ZipEntry zipentry = null;

while (E.hasmoreelements ()) {

ZipEntry = (zipentry) e.nextelement ();

System.out.println ("unziping" +zipentry.getname ());

try {

Upzip (OutputDirectory, ZipFile, ZipEntry);

} catch (Zipentryisnullex E2) {

System.out.println (E2.getmessage ());

System.out.println ("------------");

}

}

}

catch (Exception ex) {

System.out.println ("Extract File Exception" +ex.getmessage ());

Ex.printstacktrace ();

}

}

private static void Upzip (String outputdirectory, Org.apache.tools.zip.ZipFile ZipFile, Org.apache.tools.zip.ZipEntry ZipEntry) throws IOException, Zipexception, FileNotFoundException, Zipentryisnullex {

if (Zipentry.isdirectory ()) {

String Name=zipentry.getname ();

Name=name.substring (0,name.length ()-1);//For Del Fesyegeor

Mkdirs (Outputdirectory+file.separator+name);

SYSTEM.OUT.PRINTLN ("Create directory:" +outputdirectory+file.separator+name);

}else{//file Entry O9o

String Name=zipentry.getname ();

String dir = name.substring (0,name.lastindexof ("/"));

Mkdirs (Outputdirectory+file.separator+dir);

System.out.println ("Create file:" +outputdirectory+file.separator+name);

File F=new file (Outputdirectory+file.separator+zipentry.getname ());

F.createnewfile ();

InputStream in = Zipfile.getinputstream (ZipEntry);

if (in==null)

throw new Zipentryisnullex ("Zipentryisnullex:" +name);

FileOutputStream out=new FileOutputStream (f);

int C;

Byte[] By=new byte[1024];

while ((C=in.read ())! =-1) {

Out.write (BY,0,C);

}

Out.close ();

In.close ();

}

}

6. Reference

Apache ant package for zip file compression-Erase glitz, settling depth-iteye technology website. htm

Compression and decompression program based on Apache Zip package _crusoe_ Sina Blog

  • 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.