实现Java代码压缩解压

原因:某个时间想对服务器上的zip中的某些文件进行修改
本来以为很简单的事情,在网上找了好些代码,结果效果都不是很理想。


实现对象:对各种类型的文件夹(包含子文件或子文件夹)


下面介绍一下自己综合网上代码自己写的

首先要倒入一个jar包:(零积分下载)

package com.tzx.test2;

import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.IOException;  

import org.apache.tools.zip.ZipOutputStream;  

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;  
 
/**
 * 压缩解压zip文件
 * @author STVEN_KING
 * @date: 2015年4月1日 下午4:09:11
 * */
public class ZipUtil {  
  
	/**
	 * 压缩文件夹
	 * @param filesDirth  目标文件  E:/a/b
	 * @param zipFilePath  存放压缩文件的路径名 E:/a/b.zip
	 * @param deleteFile 是否删除文件夹及目录
	 * @return boolean  是否压缩成功
	 * */
    public static boolean doZip(String filesDirPath, String zipFilePath,boolean deleteFile) {  
        return doZip(new File(filesDirPath), zipFilePath,deleteFile);  
    }  
  
    private static boolean doZip(File inputFile, String zipFileName,boolean deleteFile) {  
        ZipOutputStream out = null;  
        try {  
            out = new ZipOutputStream(new FileOutputStream(zipFileName));  
            boolean result = doZip(out, inputFile, "");
            return result;  
        } catch (FileNotFoundException ex) {  
            ex.printStackTrace();  
            return false;  
        } catch (IOException ex) {  
            ex.printStackTrace();  
            return false;  
        } finally {  
            try {  
                out.close();  
                if (deleteFile) {
                	deleteDir(inputFile);
				}
            } catch (IOException ex) {  
                ex.printStackTrace();  
                return false;  
            }  
        }  
    }  
  
    private static boolean doZip(ZipOutputStream out, File f, String base) {  
        try {  
            if (f.isDirectory()) {  
                File[] fl = f.listFiles();  
                if (fl.length == 0) {
                    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
                }
                for (int i = 0; i < fl.length; i++) {  
                    doZip(out, fl[i], base + "/" + fl[i].getName());  
                }  
            } else {  
                out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));  
                FileInputStream in = new FileInputStream(f);  
                int b;  
                while ((b = in.read()) != -1) {  
                    out.write(b);  
                }  
                in.close();  
            }  
            return true;  
        } catch (IOException ex) {  
            ex.printStackTrace();  
            return false;  
        }  
    }  
  
    /**
     * 解压zip文件
     * @param zipFilePath  压缩文件路径名  E:/b/a.zip
     * @param base  解压文件存放路径  E:/b/c
     * @param deleteFile 是否删除压缩包
     * @return boolean 是否解压成功
     * */
    public static boolean unZip(String zipFilePath, String base, boolean deleteFile) {  
        try {  
            File file = new File(zipFilePath);  
            if (!file.exists()) {  
                throw new RuntimeException("解压文件不存在!");  
            }  
            ZipFile zipFile = new ZipFile(file);  
            Enumeration e = zipFile.getEntries();  
            while (e.hasMoreElements()) {  
                ZipEntry zipEntry = (ZipEntry) e.nextElement();  
                if (zipEntry.isDirectory()) {  
                    String name = zipEntry.getName();  
                    name = name.substring(0, name.length() - 1);  
                    File f = new File(base+ "/" + name);  
                    f.mkdirs();  
                } else {  
                    File f = new File(base + zipEntry.getName());  
                    f.getParentFile().mkdirs();  
                    f.createNewFile();  
                    InputStream is = zipFile.getInputStream(zipEntry);  
                    FileOutputStream fos = new FileOutputStream(f);  
                    int length = 0;  
                    byte[] b = new byte[1024];  
                    while ((length = is.read(b, 0, 1024)) != -1) {  
                        fos.write(b, 0, length);  
                    }  
                    is.close();  
                    fos.close();  
                }  
            }  
            if (zipFile != null) {  
                zipFile.close();  
            }  
            if (deleteFile) {  
                file.deleteOnExit();
            }  
            return true;  
        } catch (IOException ex) {  
            return false;  
        }  
    }
    
    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean 
     */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();//递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空,可以删除
        return dir.delete();
    }
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值