Java实现文件解压缩

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipInputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;



/**
 * 文件解压缩,需要依赖ant.jar
 *
 */
public class Zip{
    private ZipInputStream  zipIn;      //解压Zip
    private ZipOutputStream zipOut;     //压缩Zip
    private java.util.zip.ZipEntry        zipEntry;
    private int      bufSize;    //size of bytes
    private byte[]          buf;
    private int             readedBytes;
    
    /**
     * 默认buf:1024
     */
    public Zip(){
        this(1024);
    }
    /**
     * 带缓冲大小参数的构造方法
     * @param bufSize
     */
    public Zip(int bufSize){
        this.bufSize = bufSize;
        this.buf = new byte[this.bufSize];
    }
    
    /**
     * 压缩文件或文件夹
     * @param zipDirectory 文件或文件夹的路径
     * @param zipFileName 压缩后的文件名,如果为null,
       则文件是文件的名称+.zip;文件夹是文件夹的名+.zip
     */
    public void doZip(String zipDirectory,String zipFileName){
        //zipDirectoryPath:需要压缩的文件夹名
        File zipDir = new File(zipDirectory);
        if(zipFileName==null)
        	zipFileName = zipDir.getName() + ".zip";
        if(zipFileName.indexOf(".zip")==-1)
        	zipFileName+=".zip";
        String zipPath="";
        try{
	        if(zipDir.getParentFile()!=null){
	        	zipPath=zipDir.getParentFile().getCanonicalPath()+File.separator;
	        }
	        File zipOutFile=new File(zipPath+zipFileName);
	        
            zipOut = new ZipOutputStream(new BufferedOutputStream(new 
            FileOutputStream(zipOutFile)));
            zipOut.setEncoding("gbk");
            handleDir(zipDir , zipOut,zipPath);
            zipOut.close();
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
    }


    private void handleDir(File dir , ZipOutputStream zipOut,String basePath) 
        throws IOException{
        FileInputStream fileIn;
        
        if(dir.isDirectory()){
        	File[] files;
        	files = dir.listFiles();
            if(files.length == 0){//如果目录为空,则单独创建之.
                //ZipEntry的isDirectory()方法中,目录以"/"结尾.
            	zipOut.putNextEntry(new ZipEntry(dir.getCanonicalPath().
                substring(basePath.length()) + "/"));
                zipOut.closeEntry();
            }else{//如果目录不为空,则分别处理目录和文件.
                for(File fileName : files){
                	handleDir(fileName,zipOut,basePath);
                }
            }
        }else{
        	fileIn = new FileInputStream(dir);
            zipOut.putNextEntry(new ZipEntry(dir.getCanonicalPath().
            substring(basePath.length())));
            while((readedBytes = fileIn.read(buf))>0){
                zipOut.write(buf , 0 , readedBytes);
            }
            fileIn.close();
            zipOut.closeEntry();
        }
    }

    /**
     * 解压zip包
     * @param unZipfileName 需要解压的zip文件路径名
     */
    public void unZip(String unZipfileName){
        FileOutputStream fileOut;
        File file;
        
		try {
			String basePath=new File(unZipfileName).getParentFile().getCanonicalPath();
			zipIn = new ZipInputStream(new BufferedInputStream(
					new FileInputStream(unZipfileName)));
			while ((zipEntry =zipIn.getNextEntry()) != null) {
				file = new File(basePath+File.separator+zipEntry.getName());
				if (zipEntry.isDirectory()) {
					file.mkdirs();
				} else {
					File parent = file.getParentFile();
					if (!parent.exists()) {
						parent.mkdirs();
					}
					fileOut = new FileOutputStream(file);
					while ((readedBytes = zipIn.read(buf)) > 0) {
						fileOut.write(buf, 0, readedBytes);
					}
					fileOut.close();
				}
				zipIn.closeEntry();
			}
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
    }

    /**
     * 设置缓冲大小
     * @param bufSize
     */
    public void setBufSize(int bufSize){
        this.bufSize = bufSize;
    }


    public static void main(String[] args)throws Exception{
		Zip zip = new Zip();
		String file1 = "C:\\ggg";
		zip.doZip(file1,null);
    }
}

猜你喜欢

转载自sdkd-tiger.iteye.com/blog/1308689