Compressed uncompressed folder file--solve the Chinese garbled problem

Source: Internet
Author: User
Tags create directory

 

Package com.scout.util; Import Java.io.BufferedInputStream; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import Java.io.InputStream; Import java.util.Enumeration; Import Org.apache.tools.zip.ZipEntry; Import Org.apache.tools.zip.ZipFile; Import Org.apache.tools.zip.ZipOutputStream; public class Ziputil {/** * Features: Compressed into zip * @author Wu Xiaozong added to 2008-5-19 * @param zipfilep Compressed ZIP file name * @param path compression path * @thro WS Java.lang.Exception */public static void Zip (string zipfilep, String path) throws Exception {System.out.println ("compressed file Begin to do .............. File ZipFile = new file (ZIPFILEP); if (!zipfile.exists ()) {zipfile.createnewfile ();} Zipoutputstream out = new Zipoutputstream (new FileOutputStream (ZipFile)); Write (out, Path, ""); Out.close (); System.out.println ("End of compressed file.") "); } /** * Function: Write compression stream * @author Wu Xiaozong added to 2008-5-19 * @param out compressed output stream * @param path compression path * @param base-Compressed Basic directory * @throws Java.lang . Exception * * private static void write (Zipoutputstream out, string path, string base) throws Exception {File File = new file (path); if (File.isdirectory ()) {//folder, recursive base = b Ase.length () = = 0? "": Base + file.separator; file[] Tempfiles = File.listfiles (); for (int i = 0; i < tempfiles.length i++) {write (out, Tempfiles[i].getpath (), Base+tempfiles[i].getname ());} else {//file, compressed byte[] buff = new byte[2048]; int bytesread =-1; ZipEntry entry = new ZipEntry (base); Out.putnextentry (entry); InputStream in = new Bufferedinputstream (new FileInputStream (file)); while ( -1!= (bytesread = in.read (buff, 0, buff.length))) {out.write (buff, 0, bytesread);} in.close (); Out.flush (); }/** * Features: Uncompressed * @author Wu Xiaozong added to 2008-5-19 * @param zipfilename zip file * @param destpath Decompression path * @throws java.lang.Except Ion/public static void UnZip (String zipfilename, String destpath) throws Exception {System.out.println ("Unpack file start ... ............."); ZipFile zip = new ZipFile (zipfilename, "GB2312"); Enumeration ENU = zip.getentries ()//Get all Files in compressed folder WHile (Enu.hasmoreelements ()) {ZipEntry entry = (zipentry) enu.nextelement (); String file = DestPath + entry.getname (); Write (Zip, entry, file); SYSTEM.OUT.PRINTLN ("Unzip file end.") "); } /** * Function: Writes the uncompressed file to the directory * @author Wu Xiaozong added to 2008-5-19 * @param zip compressed file * @param entry Compressed file entity--compressed file in file * @param filelocation File path after decompression * @throws Exception */private static void write (ZipFile zip, zipentry entry, String file) throws Exception { if (Entry.isdirectory ()) {file F = new file (file); F.mkdirs ();} else {file F = new file (file); Createdir (f); FileOutputStream fos = new FileOutputStream (f); byte[] buffer = new byte[8196]; InputStream is = Zip.getinputstream (entry); for (int len = is.read (buffer, 0, buffer.length), Len!=-1; len = is.read (buffer, 0, 8196)) {fos.write (buffer, 0, Len); } fos.close (); }/** * Feature: Create directory * @author Wu Xiaozong added to 2008-5-19 * @param file or directory/private static void Createdir (file file) {if (FILE.ISD Irectory () &&!file.exists ()) {file.mkdirs ();} else{String Path = file.gEtpath (); int i = Path.lastindexof (file.separator); Path = path.substring (0,i); New File (Path). Mkdirs (); /** * Features: Test sample * @author Wu Xiaozong added to 2008-5-19 * @param args * * public static void Main (string[) args) {try {zip ("F://con G.zip "," f://Information "); UnZip ("F://cong.zip", "f://cong//"); catch (Exception e) {e.printstacktrace ();}} }

This source file can be used directly, with the following key points:
1 to solve the Chinese garbled problem, need ant under the tool Toolkit, as long as the Ant.jar into the classpath can be.
2 decompression, the resulting compressed entity is a file, create a file, you need to create a folder.

Ant.jar Package Download Address:

Http://jar.cn13e.com/details.action?id=43&keywords=ant

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.