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();
gzip.close(); return bos.toByteArray();
byte[] ret = bos.toByteArray(); }
bos.close(); catch (IOException e) {
return ret; LOG.error("compress data [{}] error", data, e);
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 = -1; int num;
ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((num = gzip.read(buf)) != -1) {
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num); bos.write(buf, 0, num);
} }
gzip.close();
bis.close();
byte[] ret = bos.toByteArray();
bos.flush(); bos.flush();
bos.close(); return bos.toByteArray();
return ret; }
catch (IOException e) {
LOG.error("decompress zip data error", e);
throw e;
}
} }
public static String base64DecodeDecompress(String data, String charsetName) throws IOException { public static String base64DecodeDecompress(String data, String charsetName) throws IOException {

Loading…
Cancel
Save