1. 程式人生 > 程式設計 >基於Java向zip壓縮包追加檔案

基於Java向zip壓縮包追加檔案

這篇文章主要介紹了基於Java向zip壓縮包追加檔案,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

有個需求,從某個介面下載的一個zip壓縮包,往裡面新增一個說明檔案。搜尋了一下,沒有找到往zip直接新增檔案的方法,最終解決方法是先解壓、再壓縮。

具體過程如下:

1、一個zip檔案的壓縮和解壓工具類

pom.xml加入依賴包,如下:

  <dependency>
   <groupId>org.apache.ant</groupId>
   <artifactId>ant</artifactId>
   <version>1.10.7</version>
  </dependency>

工具類程式碼:

package com.example.demo;

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,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));
   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,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;
 }
 }

2、測試

有如下目錄結構:

D:\測試\文件.zip

D:\測試\說明.pdf

把“說明.pdf”新增到“文件.zip”裡面,生成一個新壓縮包“文件(新).zip”。

package com.example.demo;

import java.io.File;
import java.util.List;

public class ZipUtilTest {
 public static void main(String[] args) {
  //解壓
  List<String> files = ZipUtil.unzip("D:/測試/文件.zip","D:/測試/");
  //集合新增檔案
  files.add("D:/測試/說明.pdf");
  //壓縮
  ZipUtil.zip(files,"D:/測試/文件(新).zip");
  //保留說明.pdf
  files.remove(files.size()-1);
  //刪除上面解壓出來的檔案
  for(String f : files){
   File file = new File(f);
   if(file.exists()){
    file.delete();
   }
  }
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。