java.util.zipを使ってzip圧縮と解凍を実現します。
転載:http://wintys.blog.51cto.com/425414/90872
import java.io.*;
import java.util.zip.*;
/**
* :zip 、
* : ZipOutputStream ZipInputStream zip .
* : java.util.zip , zip ,
* :"Exception in thread "main " java.lang.IllegalArgumentException
* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
* :
* 1、 import java.util.zip.ZipInputStream ZipOutputStream.
* java.util.zip UTF-8,Ant .
* 2、 Apache Ant zip 。
* java.util.zip , ant.jar classpath .
* import org.apache.tools.zip.*;
*
* .
*
*@author Winty
*@date 2008-8-3
*@Usage:
* :java Zip -zip "directoryName"
* :java Zip -unzip "fileName.zip"
*/
public class Zip{
private ZipInputStream zipIn; // Zip
private ZipOutputStream zipOut; // 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];
}
//
public void doZip(String zipDirectory){//zipDirectoryPath:
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";// zip
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
// doZip ,
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){// , .
//ZipEntry isDirectory() , "/" .
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{// , .
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();
}
}
}
}
// zip
public void unZip(String unZipfileName){//unZipfileName zip
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{
// , .
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();
}
}
//
public void setBufSize(int bufSize){
this.bufSize = bufSize;
}
// Zip
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(" :java Zip -zip directoryName");
System.out.println(" :java Zip -unzip fileName.zip");
throw new Exception("Arguments error!");
}
}
}