Skip to content

Commit

Permalink
feat:add zero protection.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed May 21, 2024
1 parent 152aa35 commit 1f9e2ed
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,29 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author kysonli
* @date 2018/9/7 15:35
*/
public final class GzipUtil {

private static final Logger LOG = LoggerFactory.getLogger(GzipUtil.class);

private GzipUtil() {
}

public static byte[] compress(String data, String charsetName) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data.getBytes(charsetName));
gzip.finish();
gzip.close();
byte[] ret = bos.toByteArray();
bos.close();
return ret;
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
gzip.write(data.getBytes(charsetName));
gzip.finish();
return bos.toByteArray();
}
catch (IOException e) {
LOG.error("compress data [{}] error", data, e);
throw e;
}
}

public static String compressBase64Encode(String data, String charsetName) throws IOException {
Expand All @@ -56,20 +61,19 @@ public static String compressBase64Encode(byte[] byteData, String charsetName) t


public static byte[] decompress(byte[] zipData) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(zipData);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[256];
int num = -1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
bos.write(buf, 0, num);
try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
byte[] buf = new byte[256];
int num;
while ((num = gzip.read(buf)) != -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 {
Expand Down

0 comments on commit 1f9e2ed

Please sign in to comment.