/**
* 压缩多个字符串
* @param contentMap key是目录+文件名,value是内容
* @return 压缩后的字节数组
* @throws IOException 异常
*/
public static byte[] compress(Map<String,String> contentMap)throws IOException{
byte[] arrayOfByte;
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)){
for (Map.Entry<String, String> content : contentMap.entrySet()) {
zipOutputStream.putNextEntry(new ZipEntry(content.getKey()));
zipOutputStream.write(content.getValue().getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
}
zipOutputStream.flush();
zipOutputStream.finish();
arrayOfByte = byteArrayOutputStream.toByteArray();
}
return arrayOfByte;
}
Java中字符串压缩成zip文件
最新推荐文章于 2024-05-10 11:30:13 发布