feat:add zero protection.

pull/1278/head
Haotian Zhang 1 year ago
parent 152aa35e6f
commit 1f9e2ed55a

@ -24,24 +24,29 @@ import java.util.Base64;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author kysonli * @author kysonli
* @date 2018/9/7 15:35
*/ */
public final class GzipUtil { public final class GzipUtil {
private static final Logger LOG = LoggerFactory.getLogger(GzipUtil.class);
private GzipUtil() { private GzipUtil() {
} }
public static byte[] compress(String data, String charsetName) throws IOException { public static byte[] compress(String data, String charsetName) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(data.getBytes(charsetName));
gzip.write(data.getBytes(charsetName)); gzip.finish();
gzip.finish(); return bos.toByteArray();
gzip.close(); }
byte[] ret = bos.toByteArray(); catch (IOException e) {
bos.close(); LOG.error("compress data [{}] error", data, e);
return ret; throw e;
}
} }
public static String compressBase64Encode(String data, String charsetName) throws IOException { public static String compressBase64Encode(String data, String charsetName) throws IOException {
@ -56,20 +61,19 @@ public final class GzipUtil {
public static byte[] decompress(byte[] zipData) throws IOException { public static byte[] decompress(byte[] zipData) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(zipData); try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
GZIPInputStream gzip = new GZIPInputStream(bis); byte[] buf = new byte[256];
byte[] buf = new byte[256]; int num;
int num = -1; while ((num = gzip.read(buf)) != -1) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(buf, 0, num);
while ((num = gzip.read(buf, 0, buf.length)) != -1) { }
bos.write(buf, 0, num); bos.flush();
return bos.toByteArray();
}
catch (IOException e) {
LOG.error("decompress zip data error", e);
throw e;
} }
gzip.close();
bis.close();
byte[] ret = bos.toByteArray();
bos.flush();
bos.close();
return ret;
} }
public static String base64DecodeDecompress(String data, String charsetName) throws IOException { public static String base64DecodeDecompress(String data, String charsetName) throws IOException {

Loading…
Cancel
Save