Class GzipUtil
Provides static utility methods for compressing and decompressing data using the Gzip algorithm. Supports byte arrays, files, and streams.
提供使用Gzip算法压缩和解压缩数据的静态工具方法。 支持字节数组、文件和流。
Features | 主要功能:
- Byte array compression/decompression - 字节数组压缩/解压缩
- File compression/decompression - 文件压缩/解压缩
- Stream-based compression/decompression - 基于流的压缩/解压缩
- Gzip format detection - Gzip格式检测
Usage Examples | 使用示例:
// Compress byte array
byte[] compressed = GzipUtil.compress(data);
byte[] original = GzipUtil.decompress(compressed);
// Compress file
GzipUtil.compress(sourcePath, targetPath);
GzipUtil.decompress(gzipPath, outputPath);
// Check if data is gzipped
boolean isGzip = GzipUtil.isGzipped(data);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: Yes, null inputs throw NullPointerException - 空值安全: 是,null输入抛出NullPointerException
- Since:
- JDK 25, opencode-base-io V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault maximum decompressed size (256 MB) to guard against gzip bombs 默认最大解压大小(256 MB),防止gzip炸弹 -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]compress(byte[] data) Compresses a byte array using Gzip 使用Gzip压缩字节数组static byte[]compress(InputStream input) Compresses data from an InputStream using Gzip 使用Gzip压缩输入流中的数据static voidCompresses a file using Gzip 使用Gzip压缩文件static InputStreamcompressStream(InputStream input) Returns a compressed InputStream that wraps the given input 返回一个包装给定输入的压缩InputStreamstatic byte[]decompress(byte[] data) Decompresses a Gzip-compressed byte array 解压缩Gzip压缩的字节数组static byte[]decompress(byte[] data, long maxSize) Decompresses a Gzip-compressed byte array with a size limit 带大小限制解压缩Gzip压缩的字节数组static byte[]decompress(InputStream input) Decompresses data from a Gzip-compressed InputStream 从Gzip压缩的输入流中解压缩数据static byte[]decompress(InputStream input, long maxSize) Decompresses data from a Gzip-compressed InputStream with a size limit 带大小限制从Gzip压缩的输入流中解压缩数据static voiddecompress(Path source, Path target) Decompresses a Gzip file 解压缩Gzip文件static voiddecompress(Path source, Path target, long maxSize) Decompresses a Gzip file with a size limit 带大小限制解压缩Gzip文件static InputStreamdecompressStream(InputStream input) Returns a decompressed InputStream that wraps the given compressed input, with the defaultDEFAULT_MAX_DECOMPRESSED_SIZEcap on output bytes.static InputStreamdecompressStream(InputStream input, long maxDecompressedSize) Returns a decompressed InputStream that wraps the given compressed input with an explicit size cap on the decompressed output.static booleanisGzipped(byte[] data) Checks if the given byte array starts with Gzip magic bytes 检查给定的字节数组是否以Gzip魔数开头static booleanChecks if the file at the given path starts with Gzip magic bytes 检查给定路径的文件是否以Gzip魔数开头
-
Field Details
-
DEFAULT_MAX_DECOMPRESSED_SIZE
public static final long DEFAULT_MAX_DECOMPRESSED_SIZEDefault maximum decompressed size (256 MB) to guard against gzip bombs 默认最大解压大小(256 MB),防止gzip炸弹- See Also:
-
-
Method Details
-
compress
public static byte[] compress(byte[] data) Compresses a byte array using Gzip 使用Gzip压缩字节数组- Parameters:
data- the data to compress | 要压缩的数据- Returns:
- compressed data | 压缩后的数据
- Throws:
NullPointerException- if data is null | 当data为null时抛出OpenIOOperationException- if compression fails | 当压缩失败时抛出
-
compress
Compresses a file using Gzip 使用Gzip压缩文件- Parameters:
source- the source file path | 源文件路径target- the target file path | 目标文件路径- Throws:
NullPointerException- if source or target is null | 当source或target为null时抛出OpenIOOperationException- if compression fails | 当压缩失败时抛出
-
compress
Compresses data from an InputStream using Gzip 使用Gzip压缩输入流中的数据- Parameters:
input- the input stream to compress | 要压缩的输入流- Returns:
- compressed data | 压缩后的数据
- Throws:
NullPointerException- if input is null | 当input为null时抛出OpenIOOperationException- if compression fails | 当压缩失败时抛出
-
decompress
public static byte[] decompress(byte[] data) Decompresses a Gzip-compressed byte array 解压缩Gzip压缩的字节数组- Parameters:
data- the compressed data | 压缩的数据- Returns:
- decompressed data | 解压缩后的数据
- Throws:
NullPointerException- if data is null | 当data为null时抛出OpenIOOperationException- if decompression fails | 当解压缩失败时抛出
-
decompress
public static byte[] decompress(byte[] data, long maxSize) Decompresses a Gzip-compressed byte array with a size limit 带大小限制解压缩Gzip压缩的字节数组- Parameters:
data- the compressed data | 压缩的数据maxSize- the maximum decompressed size in bytes | 最大解压大小(字节)- Returns:
- decompressed data | 解压缩后的数据
- Throws:
NullPointerException- if data is null | 当data为null时抛出IllegalArgumentException- if maxSize is not positive | 当maxSize非正数时抛出OpenIOOperationException- if decompression fails or size limit exceeded | 当解压缩失败或超出大小限制时抛出
-
decompress
Decompresses a Gzip file 解压缩Gzip文件- Parameters:
source- the Gzip file path | Gzip文件路径target- the target file path | 目标文件路径- Throws:
NullPointerException- if source or target is null | 当source或target为null时抛出OpenIOOperationException- if decompression fails | 当解压缩失败时抛出
-
decompress
Decompresses a Gzip file with a size limit 带大小限制解压缩Gzip文件- Parameters:
source- the Gzip file path | Gzip文件路径target- the target file path | 目标文件路径maxSize- the maximum decompressed size in bytes | 最大解压大小(字节)- Throws:
NullPointerException- if source or target is null | 当source或target为null时抛出IllegalArgumentException- if maxSize is not positive | 当maxSize非正数时抛出OpenIOOperationException- if decompression fails or size limit exceeded | 当解压缩失败或超出大小限制时抛出
-
decompress
Decompresses data from a Gzip-compressed InputStream 从Gzip压缩的输入流中解压缩数据- Parameters:
input- the compressed input stream | 压缩的输入流- Returns:
- decompressed data | 解压缩后的数据
- Throws:
NullPointerException- if input is null | 当input为null时抛出OpenIOOperationException- if decompression fails | 当解压缩失败时抛出
-
decompress
Decompresses data from a Gzip-compressed InputStream with a size limit 带大小限制从Gzip压缩的输入流中解压缩数据- Parameters:
input- the compressed input stream | 压缩的输入流maxSize- the maximum decompressed size in bytes | 最大解压大小(字节)- Returns:
- decompressed data | 解压缩后的数据
- Throws:
NullPointerException- if input is null | 当input为null时抛出IllegalArgumentException- if maxSize is not positive | 当maxSize非正数时抛出OpenIOOperationException- if decompression fails or size limit exceeded | 当解压缩失败或超出大小限制时抛出
-
compressStream
Returns a compressed InputStream that wraps the given input 返回一个包装给定输入的压缩InputStreamThe returned stream contains gzip-compressed data read from the input. The input data is compressed in memory for simplicity and safety.
返回的流包含从输入读取的gzip压缩数据。 为了简便和安全,输入数据在内存中压缩。
- Parameters:
input- the input stream to compress | 要压缩的输入流- Returns:
- an InputStream of compressed data | 压缩数据的InputStream
- Throws:
NullPointerException- if input is null | 当input为null时抛出OpenIOOperationException- if compression fails | 当压缩失败时抛出
-
decompressStream
Returns a decompressed InputStream that wraps the given compressed input, with the defaultDEFAULT_MAX_DECOMPRESSED_SIZEcap on output bytes. 返回一个包装给定压缩输入的解压缩 InputStream,输出字节按默认DEFAULT_MAX_DECOMPRESSED_SIZE上限。- Parameters:
input- the compressed input stream | 压缩的输入流- Returns:
- an InputStream of decompressed data | 解压缩数据的 InputStream
- Throws:
NullPointerException- if input is null | 当input为null时抛出OpenIOOperationException- if wrapping fails | 当包装失败时抛出
-
decompressStream
Returns a decompressed InputStream that wraps the given compressed input with an explicit size cap on the decompressed output. 返回一个包装给定压缩输入的解压缩 InputStream,可显式指定解压输出的字节上限。V1.0.4 sec round-4 P1: pre-fix this method returned a raw
GZIPInputStreamwith no size enforcement. A 1 KB gzip bomb decompressing to 1 GB would silently OOM any caller that consumed the stream viareadAllBytes()or similar — gzip-bomb DoS. Now wraps theGZIPInputStreamwith a counting wrapper that throwsOpenIOOperationExceptiononcemaxDecompressedSizebytes are read.V1.0.4 sec round-4 P1:修复前本方法返回未受控的
GZIPInputStream。1 KB gzip bomb 解压成 1 GB 会让任何readAllBytes()类调用方 OOM —— gzip-bomb DoS。 现用计数包装包住GZIPInputStream,读到maxDecompressedSize字节即抛OpenIOOperationException。- Parameters:
input- the compressed input stream | 压缩的输入流maxDecompressedSize- maximum decompressed bytes (must be positive) | 最大解压字节数(必须为正)- Returns:
- an InputStream of decompressed data, capped at
maxDecompressedSize| 上限为maxDecompressedSize的解压 InputStream - Throws:
NullPointerException- if input is null | 当input为null时抛出IllegalArgumentException- if maxDecompressedSize is not positive | 上限非正时抛出OpenIOOperationException- if wrapping fails | 当包装失败时抛出- Since:
- opencode-base-io V1.0.4
-
isGzipped
public static boolean isGzipped(byte[] data) Checks if the given byte array starts with Gzip magic bytes 检查给定的字节数组是否以Gzip魔数开头- Parameters:
data- the data to check | 要检查的数据- Returns:
- true if data is Gzip compressed | 如果数据是Gzip压缩的返回true
- Throws:
NullPointerException- if data is null | 当data为null时抛出
-
isGzipped
Checks if the file at the given path starts with Gzip magic bytes 检查给定路径的文件是否以Gzip魔数开头- Parameters:
path- the file path to check | 要检查的文件路径- Returns:
- true if file is Gzip compressed | 如果文件是Gzip压缩的返回true
- Throws:
NullPointerException- if path is null | 当path为null时抛出OpenIOOperationException- if reading the file fails | 当读取文件失败时抛出
-