ant压缩和解压缩工具类
生活随笔
收集整理的這篇文章主要介紹了
ant压缩和解压缩工具类
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!--文件壓縮和解壓工具類--><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.7.1</version></dependency>
package com.example.demo.util;import lombok.extern.slf4j.Slf4j;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;import java.io.*;/*** 壓縮文件和解壓縮文件(公用工具類)** @author gblfy* @date 2020-10-12*/
@Slf4j
public class ZipReduceUtil {private static final int CACHE_SIZE = 1024;//文件讀取緩沖區(qū)大小private static final String CHINESE_CHARSET = "GBK"; //使用GBK編碼可以避免壓縮中文文件名亂碼//windows環(huán)境待壓縮的文件路徑public static final String wPath = "D:\\cmiip_Dir\\000000\\";//linux環(huán)境待壓縮的文件路徑public static final String linuxPath = "/app/cmiip_Dir/00000/";/*** 單元測(cè)試壓縮*/public static void main(String[] args) {//應(yīng)用待上傳zip的前綴路徑String localPath = "D:\\cmiip_Dir\\";//上傳文件種類(類型)前綴String uploadPrefix = "S";//存量和增量標(biāo)志String flag = "I";//批次號(hào)String batchNo = "0000000001";//待壓縮的指定文件目錄(文件夾)String wSourceFolder = wPath;// String lSourceFolder = linuxPath;//壓縮文件統(tǒng)一處理ZipReduceDeal(localPath, uploadPrefix, batchNo, flag, wSourceFolder);}/*** 單元測(cè)試解壓縮*/// public static void main(String[] args) throws Exception {// ZipReduceUtil jzb = new ZipReduceUtil();//// String sourceZip = "D:\\cmiip_Dir\\S_I_0000000001_020.zip";// String destDir = "D:\\cmiip_Dir\\";// jzb.unZip(destDir, sourceZip);// }/*** 壓縮文件夾為.zip到指定目錄** @param localPath 應(yīng)用待上傳zip的前綴路徑* @param uploadPrefix 上傳文件種類(類型)前綴* @param batchNo 批次號(hào)* @param flag 存量和增量的標(biāo)識(shí)* @param sourceFolder 壓縮后的.zip絕對(duì)路徑*/public static void ZipReduceDeal(String localPath, String uploadPrefix, String batchNo, String flag, String sourceFolder) {//將待壓縮的指定文件目錄(文件夾) 轉(zhuǎn)換成文件類型的目錄File fileNum = converFileTypeDir(sourceFolder);//獲取壓縮后指定文件夾下的long getlist = getlist(fileNum);//壓縮完成后的zip的輸出路徑String zipOutputFilePath = localPath + uploadPrefix + "_" + flag + "_" + batchNo + "_0" + getlist + ".zip";//壓縮完成后的zip的輸出路徑 轉(zhuǎn)換成文件類型的目錄File zipFilePath = converFileTypeDir(zipOutputFilePath);//開(kāi)始?jí)嚎s指定文件夾zip(sourceFolder, zipFilePath);}/*** 壓縮文件** @param sourceFolder 壓縮文件夾* @param zipFilePath 壓縮文件輸出路徑*/public static void zip(String sourceFolder, File zipFilePath) {log.debug("待壓縮的目錄路徑: {} 壓縮后的zip包路徑為: {}", sourceFolder, zipFilePath);OutputStream os = null;BufferedOutputStream bos = null;ZipOutputStream zos = null;try {os = new FileOutputStream(zipFilePath);bos = new BufferedOutputStream(os);zos = new ZipOutputStream(bos);// 解決中文文件名亂碼zos.setEncoding(CHINESE_CHARSET);File file = new File(sourceFolder);String basePath = null;if (file.isDirectory()) {//壓縮文件夾basePath = file.getPath();} else {basePath = file.getParent();}//壓縮文件zipFile(file, basePath, zos);} catch (Exception e) {e.printStackTrace();} finally {try {if (zos != null) {zos.closeEntry();zos.close();}if (bos != null) {bos.close();}if (os != null) {os.close();}} catch (IOException e) {e.printStackTrace();}}}/*** 壓縮文件** @param parentFile 待壓縮文件夾* @param basePath* @param zos* @throws Exception*/private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception {File[] files = new File[0];if (parentFile.isDirectory()) {files = parentFile.listFiles();} else {files = new File[1];files[0] = parentFile;}String pathName;InputStream is;BufferedInputStream bis;byte[] cache = new byte[CACHE_SIZE];for (File file : files) {if (file.isDirectory()) {pathName = file.getPath().substring(basePath.length() + 1) + File.separator;zos.putNextEntry(new ZipEntry(pathName));zipFile(file, basePath, zos);} else {pathName = file.getPath().substring(basePath.length() + 1);is = new FileInputStream(file);bis = new BufferedInputStream(is);zos.putNextEntry(new ZipEntry(pathName));int nRead = 0;while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {zos.write(cache, 0, nRead);}bis.close();is.close();}}}/*** 獲取指定文件夾下的文件數(shù)量** @param f* @return*/public static long getlist(File f) {//遞歸求取目錄文件個(gè)數(shù)log.debug("開(kāi)始從 {} 路徑下面獲取指文件數(shù)量", f);long size = 0;File flist[] = f.listFiles();size = flist.length;for (int i = 0; i < flist.length; i++) {if (flist[i].isDirectory()) {size = size + getlist(flist[i]);size--;}}return size;}/*** 將String類型的目錄轉(zhuǎn)換為文件類型的目錄** @param fileDir* @return*/public static File converFileTypeDir(String fileDir) {return new File(fileDir);}/*** 解壓縮** @param destDir 解壓縮后的目標(biāo)目錄 d:/cmiip_Dir* @param sourceZip 源zip文件 d:/cmiip_Dir/S_I_0000000001_020.zip* 結(jié)果則是 將d:/cmiip_Dir/S_I_0000000001_020.zip文件解壓縮到d:/cmiip_Dir目錄下*/public void unZip(String destDir, String sourceZip) {try {Project prj1 = new Project();Expand expand = new Expand();expand.setProject(prj1);expand.setSrc(new File(sourceZip));expand.setOverwrite(false);//是否覆蓋File f = new File(destDir);expand.setDest(f);expand.execute();} catch (Exception e) {e.printStackTrace();}}
}
總結(jié)
以上是生活随笔為你收集整理的ant压缩和解压缩工具类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ElasticSearch docker
- 下一篇: npm ERR! cb() never