java向一个压缩包里增加文件

     如果遇到,向现有的压缩包里增加文件的需求可以参照如下的方式:

思路:1、先将压缩包解压

           2、删除旧的压缩包

           3、将解压后的文件和希望添加的文件一起重新生成一个压缩包

           4、将第一步中解压后的文件删除。

 

一、依赖

  <!-- 文件压缩,解压 -->
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.7</version>
        </dependency>

二、工具类

 
package com.test;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipException;

import org.apache.tools.zip.*;

public class ZipUtil {

    private static int BUFFERSIZE = 1024;

    /**
     * 压缩
     *
     * @param paths
     * @param fileName
     */
    public static void zip(List<String> paths, String fileName) {
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(fileName));
            for (String filePath : paths) {
                // 递归压缩文件
                File file = new File(filePath);
                String relativePath = file.getName();
                if (file.isDirectory()) {
                    relativePath += File.separator;
                }
                zipFile(file, relativePath, zos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void zipFile(File file, String relativePath, ZipOutputStream zos) {
        InputStream is = null;
        try {
            if (!file.isDirectory()) {
                ZipEntry zp = new ZipEntry(relativePath);
                zos.putNextEntry(zp);
                is = new FileInputStream(file);
                byte[] buffer = new byte[BUFFERSIZE];
                int length = 0;
                while ((length = is.read(buffer)) >= 0) {
                    zos.write(buffer, 0, length);
                }
                zos.setEncoding("gbk");//解决文件名中文乱码
                zos.flush();
                zos.closeEntry();
            } else {
                String tempPath = null;
                for (File f : file.listFiles()) {
                    tempPath = relativePath + f.getName();
                    if (f.isDirectory()) {
                        tempPath += File.separator;
                    }
                    zipFile(f, tempPath, zos);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解压缩
     *
     * @param fileName
     * @param path
     */
    public static List<String> unzip(String fileName, String path) {
        FileOutputStream fos = null;
        InputStream is = null;
        List<String> filePaths = new ArrayList<String>();
        try {
            ZipFile zf = new ZipFile(new File(fileName), "GBK");
            Enumeration<?> en = zf.getEntries();
            while (en.hasMoreElements()) {
                ZipEntry zn = (ZipEntry) en.nextElement();
                if (!zn.isDirectory()) {
                    is = zf.getInputStream(zn);
                    File f = new File(path  + zn.getName());
                    File file = f.getParentFile();
                    file.mkdirs();
                    fos = new FileOutputStream(path  + zn.getName());
                    int len = 0;
                    byte bufer[] = new byte[BUFFERSIZE];
                    while (-1 != (len = is.read(bufer))) {
                        fos.write(bufer, 0, len);
                    }
                    fos.close();
                    filePaths.add(path + zn.getName());
                }
            }
        } catch (ZipException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
                if (null != fos) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filePaths;
    }
}

三、调用方法

 

package com.test;
 
import java.io.File;
import java.util.List;
 
public class ZipUtilTest {
    public static void main(String[] args) {
        //解压
        List<String> files = ZipUtil.unzip("D:/test.zip", "D:/");
        //增加想要向压缩包里增加的文件
        files.add("D:/1.txt");
        //删除以前的压缩包
        File oldZipfile = new File("D:/test.zip");
        if(oldZipfile.exists()){
            oldZipfile.delete();
        }
        //生成新的压缩包
        ZipUtil.zip(files,"D:/test.zip");
        
        //保留注D:/1.txt,这里不从List里删除,最后一步就会把文件删除
        files.remove(files.size()-1);
        
        //删除上面解压出来的文件
        for(String f : files){
            File file = new File(f);
            if(file.exists()){
                file.delete();
            }
        }
    }
}
    

 

完结赶紧试试吧!

 

资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
posted @ 2022-07-05 18:40  万笑佛  阅读(1260)  评论(0编辑  收藏  举报