云盘资源分享论坛

 找回密码
 立即注册
搜索
热搜: 书籍 电影 音乐
查看: 196|回复: 0

java 压缩文件算法有哪些?

[复制链接]

966

主题

156

回帖

4492

积分

中级会员

Rank: 3Rank: 3

UID
32013
金钱
3371
钻石
7
积分
4492
注册时间
2023-7-27
发表于 2023-8-2 13:22:59 | 显示全部楼层 |阅读模式

  • Deflater/Inflater:
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:
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:
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);            }        }    }}
盘基地论坛免责声明
1、本站资源来自互联网用户收集发布,仅供用于学习和交流。
2、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。
3、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决。
4、联系邮箱:admin@panjdzy.com
5、官方网址:www.panjdzy.com
6、备用网址:www.panjd.top




上一篇:char数据类型
下一篇:VBA编程使用教程
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|云盘资源分享论坛

GMT+8, 2025-6-25 15:27

Powered by Discuz!    联系邮箱:admin@panjdzy.com

本站资源来自互联网用户收集发布,仅供用于学习和交流。

如有侵权之处,请联系站长并出示版权证明以便删除,敬请谅解!

快速回复 返回顶部 返回列表