apache. comons. copress 압축 및 가압 파일 도구 류 사용 하기
13020 단어 Java
package com.my.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import java.io.*;
/**
*
*
* @author helloworld
*/
@Slf4j
public class ZipUtil {
/**
* zip
*
* @param files
* @param zipFile
* @param deleteFileAfterZip ,
* @return
*/
public static boolean compressFiles2Zip(List files, File zipFile, boolean deleteFileAfterZip) {
InputStream inputStream = null;
ZipArchiveOutputStream zipArchiveOutputStream = null;
try {
zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);
//Use Zip64 extensions for all entries where they are required
zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);
for (File file : files) {
// ZipArchiveEntry , ZipArchiveOutputStream
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024 * 5];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
// ZipArchiveEntry
zipArchiveOutputStream.write(buffer, 0, len);
}
}
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.finish();
if (deleteFileAfterZip) {
for (File file : files) {
file.deleteOnExit();
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
//
if (null != inputStream) {
inputStream.close();
}
//
if (null != zipArchiveOutputStream) {
zipArchiveOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* zip
*
* @param zipFilePath
* @param targetDirPath
* @return
*/
public static boolean decompressZip2Files(String zipFilePath, String targetDirPath) {
InputStream inputStream = null;
OutputStream outputStream = null;
//zip
ZipArchiveInputStream zipArchiveInputStream = null;
ArchiveEntry archiveEntry = null;
try {
File zipFile = new File(zipFilePath);
inputStream = new FileInputStream(zipFile);
zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8");
while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) {
//
String archiveEntryFileName = archiveEntry.getName();
//
String archiveEntryPath = targetDirPath + archiveEntryFileName;
//
File entryFile = new File(archiveEntryPath);
if (!entryFile.exists()) {
boolean mkdirs = entryFile.getParentFile().mkdirs();
}
byte[] buffer = new byte[1024 * 5];
outputStream = new FileOutputStream(entryFile);
int len = -1;
while ((len = zipArchiveInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (null != outputStream) {
outputStream.close();
}
if (null != zipArchiveInputStream) {
zipArchiveInputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* zip
*
* @param docs
* @param targetFile
* @return
*/
public static boolean compressDoc2Zip(List> docs, File targetFile) throws IOException {
List fileList = new ArrayList<>();
for (Pair doc : docs) {
String name = doc.getFirst();
XWPFDocument document = doc.getSecond();
File file = convertDocument2File(document, name);
fileList.add(file);
}
return compressFiles2Zip(fileList, targetFile, true);
}
/**
* XWPFDocument File
*
* @param document
* @param fileName file
* @return file
* @throws IOException
*/
public static File convertDocument2File(XWPFDocument document, String fileName) throws IOException {
if (fileName.length() <= 3) {
fileName += "-" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
}
if (!fileName.endsWith("docx")) {
fileName += ".docx";
}
File file = createTempFile(fileName);
try (OutputStream os = new FileOutputStream(file)) {
document.write(os);
}
return file;
}
/**
*
*/
public static File createTempFile(String fullFileName) throws IOException {
String tempDirectoryPath = FileUtils.getTempDirectoryPath();
File file = new File(tempDirectoryPath + File.separator + fullFileName);
file.deleteOnExit();
boolean newFile = file.createNewFile();
log.debug("newFile {} => {}", fullFileName, newFile);
return file;
}
public static void main(String[] args) {
File f1 = new File("/Users/my/Desktop/WechatIMG78.jpeg");
File f2 = new File("/Users/my/Desktop/WechatIMG79.jpeg");
File f3 = new File("/Users/my/Desktop/aaaaa.docx");
//
boolean b = compressFiles2Zip(new File[]{f1, f2, f3}, "/Users/my/Desktop/haha.zip");
log.info("compressFiles2Zip={}", b);
//
boolean b1 = decompressZip2Files("/Users/my/Desktop/haha.zip", "/Users/zhangbaozhen/my/hahaha/");
log.info("decompressZip2Files={}", b1);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.