com.blr19c.common.io.ZipUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
The blr19c-common library contains utility classes, Map/Stream implementation, sending enterprise
WeChat, IP/ip address acquisition, mybatis-plus separation implementation, and so on.
package com.blr19c.common.io;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* zip工具
*
* @author blr
*/
public class ZipUtils {
/**
* 转换为zip
*
* @param zips 压缩数据
* @param method 压缩等级
*/
public static byte[] toZip(List zips, int method) throws IOException {
ByteArrayOutputStream fn = new ByteArrayOutputStream();
toZip(zips, method, fn);
return fn.toByteArray();
}
/**
* 转换为zip
*
* @param zips 压缩数据
* @param method 压缩等级
* @param out 目标
*/
public static void toZip(List zips, int method, OutputStream out) throws IOException {
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new CheckedOutputStream(out, new CRC32()))) {
zipOutputStream.setMethod(method);
for (Zip zip : zips) {
ZipEntry zipEntry = new ZipEntry(zip.name);
InputStream resource = zip.resource;
zipEntry.setSize(resource.available());
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(IOUtils.toByteArray(resource));
zip.close();
}
zipOutputStream.finish();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy