Using Java.util.zip to achieve file compression and decompression

Source: Internet
Author: User
Tags zip

Import java.io.*;
Import java.util.zip.*;
/** *//**
* Function: Zip compression, decompression
* Description: This program through Zipoutputstream and zipinputstream implementation of the ZIP compression and decompression function.
* Problem: Because the Java.util.zip package does not support Chinese characters, when the zip file has a file with the name Chinese
, author by http://www.bt285.cn http://www.5a520.cn
* There will be an exception: "Exception in Thread" main "java.lang.illegalargumentexceptionbr> *" Java.util.zip.Zip Inputstream.getutf8string (zipinputstream.java:285)
Solution
* Method 1, modify import Java.util.zip.ZipInputStream and Zipoutputstream.
* Java.util.zip only supports utf-8,ant to specify encoding.
* Method 2, using the Zip tool provided in Apache Ant.
* Do not use java.util.zip bag, put the Ant.jar into the classpath.
* Use import org.apache.tools.zip.* in the program;
*
* For programming learning reference only.
*
* @author Winty
* @date 2008-8-3
* @Usage:
* Compression: Java zip-zip "DirectoryName"
* Decompression: Java zip-unzip "Filename.zip"
*/

public class zip{
Private Zipinputstream Zipin; Extract zip
Private Zipoutputstream zipout; Compressed zip
Private ZipEntry ZipEntry;
private static int bufsize; Size of bytes
Private byte[] BUF;
private int readedbytes;

Public Zip () {
This (512);
}

Public Zip (int bufsize) {
This.bufsize = bufsize;
This.buf = new Byte[this.bufsize];
}

Compress files within a folder
public void Dozip (String zipdirectory) {//zipdirectorypath: folder name to compress
File file;
File Zipdir;

Zipdir = new File (zipdirectory);
String zipfilename = zipdir.getname () + ". zip";//ZIP file name generated after compression

try{
This.zipout = new Zipoutputstream (new Bufferedoutputstream (New FileOutputStream (Zipfilename)));
Handledir (Zipdir, this.zipout);
This.zipOut.close ();
}catch (IOException IoE) {
Ioe.printstacktrace ();
}
}

Called by Dozip, recursively completes the directory file read
private void Handledir (File dir, Zipoutputstream zipout) throws ioexception{
FileInputStream Filein;
file[] files;

Files = Dir.listfiles ();

if (Files.length = = 0) {//If the directory is empty, it is created separately.
In the Isdirectory () method of ZipEntry, the directory ends with "/".
This.zipOut.putNextEntry (New ZipEntry (dir.tostring () + "/"));
This.zipOut.closeEntry ();
}
else{//If the directory is not empty, the directories and files are processed separately.
for (File filename:files) {
System.out.println (FileName);

if (Filename.isdirectory ()) {
Handledir (FileName, this.zipout);
}
else{
Filein = new FileInputStream (fileName);
This.zipOut.putNextEntry (New ZipEntry (Filename.tostring ()));

while ((This.readedbytes = Filein.read (this.buf)) >0) {
This.zipOut.write (this.buf, 0, this.readedbytes);
}

This.zipOut.closeEntry ();
}
}
}
}

Extract the specified zip file
public void UnZip (String unzipfilename) {//unzipfilename zip file name to extract
FileOutputStream fileout;
File file;

try{
This.zipin = new Zipinputstream (new Bufferedinputstream (New FileInputStream (Unzipfilename)));

while ((This.zipentry = This.zipIn.getNextEntry ())!= null) {
File = new file (This.zipEntry.getName ());
System.out.println (file);///

if (This.zipEntry.isDirectory ()) {
File.mkdirs ();
}
else{
If the directory for the specified file does not exist, it is created.
File parent = File.getparentfile ();
if (!parent.exists ()) {
Parent.mkdirs ();
}

Fileout = new FileOutputStream (file);
while ((This.readedbytes = This.zipIn.read (this.buf)) > 0) {
Fileout.write (this.buf, 0, this.readedbytes);
}
Fileout.close ();
}
This.zipIn.closeEntry ();
}
}catch (IOException IoE) {
Ioe.printstacktrace ();
}
}

Set Buffer size
public void setbufsize (int bufsize) {
This.bufsize = bufsize;
}

Testing the Zip class
public static void Main (string[] args) throws exception{
if (args.length==2) {
String name = Args[1];
Zip zip = new zip ();

if (Args[0].equals ("-zip"))
Zip.dozip (name);
else if (args[0].equals ("-unzip"))
Zip.unzip (name);
}
else{
System.out.println ("Usage:");
System.out.println ("Compression: Java zip-zip http://www.bt285.cn/directoryName");
System.out.println ("Decompression: Java zip-unzip http://www.feng123.com/fileName.zip");
throw new Exception ("Arguments error!");
}
}
}

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.