Deflater 类是 Java 中的压缩类,使用 DEFLATE 压缩算法将数据压缩成字节数组。Inflater 类则用于解压缩 DEFLATE 格式的数据。 压缩示例:
import java.util.zip.Deflater;public class DeflateExample { public static byte[] compress(byte[] data) { // 创建 Deflater 对象 Deflater deflater = new Deflater(); // 设置输入数据 deflater.setInput(data); deflater.finish(); // 创建一个缓冲区来存储压缩后的数据 byte[] compressedData = new byte[data.length]; int compressedLength = deflater.deflate(compressedData); // 创建一个新的数组,仅包含压缩后的数据 byte[] result = new byte[compressedLength]; System.arraycopy(compressedData, 0, result, 0, compressedLength); return result; }}解压缩示例:
import java.util.zip.Inflater;public class InflateExample { public static byte[] decompress(byte[] compressedData) { // 创建 Inflater 对象 Inflater inflater = new Inflater(); // 设置输入数据 inflater.setInput(compressedData); // 创建一个缓冲区来存储解压缩后的数据 byte[] decompressedData = new byte[compressedData.length * 2]; int decompressedLength = inflater.inflate(decompressedData); // 创建一个新的数组,仅包含解压缩后的数据 byte[] result = new byte[decompressedLength]; System.arraycopy(decompressedData, 0, result, 0, decompressedLength); return result; }}- ZipOutputStream/ZipInputStream:
ZipOutputStream 类用于将文件或数据压缩成 ZIP 格式,ZipInputStream 类用于解压缩 ZIP 格式的文件或数据。 压缩示例:
import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipExample { public static void compressFile(String sourceFilePath, String zipFilePath) throws IOException { try (FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos)) { File fileToZip = new File(sourceFilePath); FileInputStream fis = new FileInputStream(fileToZip); // 创建 ZIP 文件条目 ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zos.putNextEntry(zipEntry); // 将文件数据写入 ZIP 输出流 byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { zos.write(buffer, 0, length); } // 关闭 ZIP 条目和输入流 fis.close(); zos.closeEntry(); } }}解压缩示例:
import java.io.FileOutputStream;import java.io.InputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;public class UnzipExample { public static void decompressFile(String zipFilePath, String destDirectory) throws IOException { try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath))) { byte[] buffer = new byte[1024]; ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File newFile = new File(destDirectory + File.separator + fileName); // 创建目录或写入文件 if (zipEntry.isDirectory()) { newFile.mkdirs(); } else { FileOutputStream fos = new FileOutputStream(newFile); int length; while ((length = zis.read(buffer)) >= 0) { fos.write(buffer, 0, length); } fos.close(); } zipEntry = zis.getNextEntry(); } zis.closeEntry(); } }}BZIP2 是一种基于 Burrows-Wheeler 变换的压缩算法,通常比 GZIP 更有效率,能够产生更小的压缩文件。你可以使用 Apache Commons Compress 库来实现 BZIP2 压缩和解压缩。
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;public class Bzip2Example { public static void compressFile(String sourceFilePath, String bzip2FilePath) throws IOException { try (FileInputStream fis = new FileInputStream(sourceFilePath); FileOutputStream fos = new FileOutputStream(bzip2FilePath); BZip2CompressorOutputStream bzip2cos = new BZip2CompressorOutputStream(fos)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { bzip2cos.write(buffer, 0, length); } } } public static void decompressFile(String bzip2FilePath, String destFilePath) throws IOException { try (BZip2CompressorInputStream bzip2cis = new BZip2CompressorInputStream(new FileInputStream(bzip2FilePath)); FileOutputStream fos = new FileOutputStream(destFilePath)) { byte[] buffer = new byte[1024]; int length; while ((length = bzip2cis.read(buffer)) >= 0) { fos.write(buffer, 0, length); } } }}LZMA(Lempel-Ziv-Markov chain Algorithm)是一种基于哈夫曼编码和字典压缩的算法,具有较高的压缩比。你可以使用 XZ for Java 库来实现 LZMA 压缩和解压缩。
import org.tukaani.xz.LZMA2Options;import org.tukaani.xz.XZOutputStream;import org.tukaani.xz.XZInputStream;public class LzmaExample { public static void compressFile(String sourceFilePath, String lzmaFilePath) throws IOException { try (FileInputStream fis = new FileInputStream(sourceFilePath); FileOutputStream fos = new FileOutputStream(lzmaFilePath); XZOutputStream xzcos = new XZOutputStream(fos, new LZMA2Options())) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { xzcos.write(buffer, 0, length); } } } public static void decompressFile(String lzmaFilePath, String destFilePath) throws IOException { try (XZInputStream xzcis = new XZInputStream(new FileInputStream(lzmaFilePath)); FileOutputStream fos = new FileOutputStream(destFilePath)) { byte[] buffer = new byte[1024]; int length; while ((length = xzcis.read(buffer)) >= 0) { fos.write(buffer, 0, length); } } }}
|