import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.poi.util.IOUtils;
/**
*基于内存以ZIP算法进行压缩和解压工具类.
*/
public class ZipRamUtil {
/**
* 使用ZIP算法进行压缩.
* @param sourceFileBytesMap 待压缩文件的Map集合.
* @return 压缩后的ZIP文件字节数组.
* @throws Exception 压缩过程中可能发生的异常,若发生异常,则返回的字节数组长度为0.
*/
public static byte[] compressByZip(Map<String, byte[]> sourceFileBytesMap) throws IOException {
// 变量定义.
ZipOutputStream zipZos = null;
ByteArrayOutputStream zipBaos = null;
try {
// 压缩文件变量初始化.
zipBaos = new ByteArrayOutputStream();
zipZos = new ZipOutputStream(zipBaos);
// 将文件添加到ZIP条目中.
if (null != sourceFileBytesMap && sourceFileBytesMap.size() > 0) {
for (Map.Entry<String, byte[]> singleFile : sourceFileBytesMap.entrySet()) {
// 设置文件名
ZipEntry zipEntry = new ZipEntry(singleFile.getKey());
zipZos.putNextEntry(zipEntry);
// 写入文件
zipZos.write(singleFile.getValue());
// 停止当前文件写入
zipZos.closeEntry();
}
// 结束压缩
zipZos.finish();
} else {
zipBaos = new ByteArrayOutputStream();
}
} finally {
if (null != zipBaos){
zipBaos.close();
}
if (null != zipZos){
zipZos.close();
}
}
return zipBaos.toByteArray();
}
/**
* 使用ZIP算法进行解压.
* @param sourceZipFileBytes ZIP文件字节数组.
* @return 解压后的文件Map集合.
* @throws Exception 解压过程中可能发生的异常,若发生异常,返回Map集合长度为0.
*/
public static Map<String, byte[]> decompressByZip(byte[] sourceZipFileBytes) throws Exception {
// 变量定义.
String zipEntryName = null;
ZipEntry singleZipEntry = null;
ZipInputStream sourceZipZis = null;
BufferedInputStream sourceZipBis = null;
ByteArrayInputStream sourceZipBais = null;
Map<String, byte[]> targetFilesFolderMap = null;
try {
// 解压变量初始化.
targetFilesFolderMap = new HashMap<String, byte[]>();
sourceZipBais = new ByteArrayInputStream(sourceZipFileBytes);
sourceZipBis = new BufferedInputStream(sourceZipBais);
sourceZipZis = new ZipInputStream(sourceZipBis);
// 条目解压缩至Map中.
while ((singleZipEntry = sourceZipZis.getNextEntry()) != null) {
zipEntryName = singleZipEntry.getName();
targetFilesFolderMap.put(zipEntryName, IOUtils.toByteArray(sourceZipZis));
}
} finally {
if (null != sourceZipZis){
sourceZipZis.close();
}
if (null != sourceZipBis){
sourceZipBis.close();
}
if (null != sourceZipBais){
sourceZipBais.close();
}
}
return targetFilesFolderMap;
}
}
基于内存以ZIP进行压缩和解压工具类
于 2024-03-27 23:15:12 首次发布