亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于Java向zip壓縮包追加文件

 更新時(shí)間:2019年12月11日 14:26:18   作者:gdjlc  
這篇文章主要介紹了基于Java向zip壓縮包追加文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了基于Java向zip壓縮包追加文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

有個(gè)需求,從某個(gè)接口下載的一個(gè)zip壓縮包,往里面添加一個(gè)說(shuō)明文件。搜索了一下,沒有找到往zip直接添加文件的方法,最終解決方法是先解壓、再壓縮。

具體過(guò)程如下:

1、一個(gè)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, 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));
   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;
 }
 }

2、測(cè)試

有如下目錄結(jié)構(gòu):

D:\測(cè)試\文檔.zip

D:\測(cè)試\說(shuō)明.pdf

把“說(shuō)明.pdf”添加到“文檔.zip”里面,生成一個(gè)新壓縮包“文檔(新).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:/測(cè)試/文檔.zip", "D:/測(cè)試/");
  //集合添加文件
  files.add("D:/測(cè)試/說(shuō)明.pdf");
  //壓縮
  ZipUtil.zip(files,"D:/測(cè)試/文檔(新).zip");
  //保留說(shuō)明.pdf
  files.remove(files.size()-1);
  //刪除上面解壓出來(lái)的文件
  for(String f : files){
   File file = new File(f);
   if(file.exists()){
    file.delete();
   }
  }
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java的Hibernate框架中的緩存與二級(jí)緩存

    詳解Java的Hibernate框架中的緩存與二級(jí)緩存

    這篇文章主要介紹了Java的Hibernate框架中的緩存與二級(jí)緩存,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Java數(shù)組創(chuàng)建的3種方法6種寫法代碼示例

    Java數(shù)組創(chuàng)建的3種方法6種寫法代碼示例

    這篇文章主要給大家介紹了關(guān)于Java數(shù)組創(chuàng)建的3種方法6種寫法,在Java中我們可以使用關(guān)鍵字new來(lái)創(chuàng)建一個(gè)數(shù)組,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口

    這篇文章主要介紹了SpringBoot 如何使用Dataway配置數(shù)據(jù)查詢接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java8特性使用Function代替分支語(yǔ)句

    Java8特性使用Function代替分支語(yǔ)句

    這篇文章主要介紹了Java8特性使用Function代替分支語(yǔ)句,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 詳解Spring MVC 集成EHCache緩存

    詳解Spring MVC 集成EHCache緩存

    本篇文章主要介紹了詳解Spring MVC 集成EHCache緩存,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • springboot如何讀取自定義配置項(xiàng)

    springboot如何讀取自定義配置項(xiàng)

    這篇文章主要介紹了springboot如何讀取自定義配置項(xiàng)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Java WindowBuilder 安裝及基本使用的教程

    Java WindowBuilder 安裝及基本使用的教程

    這篇文章主要介紹了Java WindowBuilder 安裝及基本使用的教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • spring?boot學(xué)習(xí)筆記之操作ActiveMQ指南

    spring?boot學(xué)習(xí)筆記之操作ActiveMQ指南

    ActiveMQ是一種開源的基于JMS規(guī)范的一種消息中間件的實(shí)現(xiàn),ActiveMQ的設(shè)計(jì)目標(biāo)是提供標(biāo)準(zhǔn)的,面向消息的,能夠跨越多語(yǔ)言和多系統(tǒng)的應(yīng)用集成消息通信中間件,這篇文章主要給大家介紹了關(guān)于spring?boot學(xué)習(xí)筆記之操作ActiveMQ指南的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Springboot如何實(shí)現(xiàn)對(duì)配置文件中的明文密碼加密

    Springboot如何實(shí)現(xiàn)對(duì)配置文件中的明文密碼加密

    這篇文章主要介紹了Springboot如何實(shí)現(xiàn)對(duì)配置文件中的明文密碼加密問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 2022最新Java泛型詳解(360度無(wú)死角介紹)

    2022最新Java泛型詳解(360度無(wú)死角介紹)

    Java泛型(generics)是JDK5中引入的一個(gè)新特性,泛型提供了 編譯時(shí)類型安全監(jiān)測(cè)機(jī)制,該機(jī)制允許我們?cè)诰幾g時(shí)檢測(cè)到非法的類型數(shù)據(jù)結(jié)構(gòu),這篇文章主要介紹了java泛型的基本概念及使用詳解,感興趣的朋友跟隨小編一起看看吧
    2022-10-10

最新評(píng)論