GZIP压缩和解压

 #region GZip
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="inBytes"></param>
        /// <returns></returns>
        public static byte[] Compress(byte[] inBytes)
        {

            MemoryStream outStream = new MemoryStream();

            using (MemoryStream intStream = new MemoryStream(inBytes))
            {

                using (GZipStream Compress =new GZipStream(outStream, CompressionMode.Compress))
                {
                    intStream.CopyTo(Compress);
                }

            }
            return outStream.ToArray();

        }


        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="inStream"></param>
        /// <returns></returns>
        public static byte[] Decompress(byte[] inStream)
        {
            byte[] result = null;
            MemoryStream compressedStream = new MemoryStream(inStream);
            using (MemoryStream outStream = new MemoryStream())
            {
                using (GZipStream Decompress = new GZipStream(compressedStream,CompressionMode.Decompress))
                {
                    Decompress.CopyTo(outStream);
                    result = outStream.ToArray();
                }
            }
            return result;

        }
        #endregion

### 回答1: 以下是 Java 代码示例,可以实现 gzip 压缩解压。 ``` import java.io.*; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GzipUtil { public static byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(data); gzip.finish(); gzip.close(); return bos.toByteArray(); } public static byte[] decompress(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = gzip.read(buffer)) != -1) { bos.write(buffer, 0, len); } gzip.close(); return bos.toByteArray(); } } ``` 代码中的 `compress` 方法实现了 gzip 压缩,而 `decompress` 方法则实现了 gzip 解压。 ### 回答2: 可以使用Java的GZIPInputStreamGZIPOutputStream类来实现gzip压缩解压。 下面是一个用Java编写的简单示例代码: ``` import java.io.*; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GzipExample { public static void main(String[] args) { String inputFilePath = "/path/to/inputfile.txt"; String compressedFilePath = "/path/to/compressedfile.gz"; String decompressedFilePath = "/path/to/decompressedfile.txt"; // 压缩文件 compressFile(inputFilePath, compressedFilePath); // 解压文件 decompressFile(compressedFilePath, decompressedFilePath); } public static void compressFile(String inputFilePath, String compressedFilePath) { try { FileInputStream fis = new FileInputStream(inputFilePath); FileOutputStream fos = new FileOutputStream(compressedFilePath); GZIPOutputStream gzipOS = new GZIPOutputStream(fos); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) != -1) { gzipOS.write(buffer, 0, length); } gzipOS.close(); fos.close(); fis.close(); System.out.println("文件已成功压缩gzip格式!"); } catch (IOException e) { e.printStackTrace(); } } public static void decompressFile(String compressedFilePath, String decompressedFilePath) { try { FileInputStream fis = new FileInputStream(compressedFilePath); GZIPInputStream gzipIS = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(decompressedFilePath); byte[] buffer = new byte[1024]; int length; while ((length = gzipIS.read(buffer)) != -1) { fos.write(buffer, 0, length); } fos.close(); gzipIS.close(); fis.close(); System.out.println("文件已成功解压缩!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用上述代码,你可以将指定的输入文件压缩gzip格式的文件,并将其解压缩为原始文件。只需将"inputFilePath","compressedFilePath"和"decompressedFilePath"变量设置为你希望的文件路径即可。请确保在运行代码之前将这些路径更改为实际路径。 ### 回答3: Sure! 下面是用Java编写的一个使用GZIP压缩解压缩的例子: 压缩代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class GzipCompressionExample { public static void main(String[] args) { String sourceFile = "input.txt"; String gzipFile = "compressed.gz"; try (FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(gzipFile); GZIPOutputStream gzipOS = new GZIPOutputStream(fos)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { gzipOS.write(buffer, 0, bytesRead); } System.out.println("File compressed successfully!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 解压代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; public class GzipDecompressionExample { public static void main(String[] args) { String gzipFile = "compressed.gz"; String outputFile = "output.txt"; try (FileInputStream fis = new FileInputStream(gzipFile); GZIPInputStream gzipIS = new GZIPInputStream(fis); FileOutputStream fos = new FileOutputStream(outputFile)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = gzipIS.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } System.out.println("File decompressed successfully!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码基于Java 8的try-with-resources语法实现了GZIP压缩解压缩。需要注意的是,压缩文件中需要进行解压数据的大小会比原始文件更小。另外,确保输入文件存在,并提供正确的文件路径。 希望这可以帮到你!如果你对这方面还有什么疑问,欢迎继续提问哦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值