java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
来源:互联网 发布:logitech g502 mac 编辑:程序博客网 时间:2024/05/29 04:33
全栈工程师开发手册 (作者:栾鹏)
java教程全解
java实现gzip压缩解压缩byte[]字节数组,文件,字符串。
测试代码
public static void main(String[] args) { try { //测试字符串 String inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org"; System.err.println("原文:\t" + inputStr); byte[] input = inputStr.getBytes(); System.err.println("长度:\t" + input.length); byte[] data = GZipUtils.compress(input); System.err.println("压缩后:\t"); System.err.println("长度:\t" + data.length); byte[] output = GZipUtils.decompress(data); String outputStr = new String(output); System.err.println("解压缩后:\t" + outputStr); System.err.println("长度:\t" + output.length); //测试文件 FileOutputStream fos = new FileOutputStream("test.txt"); fos.write(inputStr.getBytes()); fos.flush(); fos.close(); GZipUtils.compress("test.txt", false); GZipUtils.decompress("test.txt.gz", false); File file = new File("test.txt"); FileInputStream fis = new FileInputStream(file); DataInputStream dis = new DataInputStream(fis); byte[] data1 = new byte[(int) file.length()]; dis.readFully(data1); fis.close(); outputStr = new String(data1); System.err.println("解压缩后:\t" + outputStr); } catch (Exception e) { e.printStackTrace(); } }
gzip压缩解压缩工具类的实现
package com.lp.app.io;import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; //GZIP压缩解压缩工具 public abstract class GZipUtils { //数据压缩 public static byte[] compress(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 压缩 compress(bais, baos); byte[] output = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); return output; } //文件压缩 public static void compress(File file) throws Exception { compress(file, true); } //文件压缩 是否删除原始文件 public static void compress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath() + ".gz"); compress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } //数据压缩 public static void compress(InputStream is, OutputStream os) throws Exception { GZIPOutputStream gos = new GZIPOutputStream(os); int count; byte data[] = new byte[1024]; while ((count = is.read(data, 0, 1024)) != -1) { gos.write(data, 0, count); } gos.finish(); gos.flush(); gos.close(); } //文件压缩 默认删除源文件 public static void compress(String path) throws Exception { compress(path, true); } //文件压缩 是否删除原始文件 public static void compress(String path, boolean delete) throws Exception { File file = new File(path); compress(file, delete); } //数据解压缩 public static byte[] decompress(byte[] data) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(data); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 解压缩 decompress(bais, baos); data = baos.toByteArray(); baos.flush(); baos.close(); bais.close(); return data; } //文件解压缩 public static void decompress(File file) throws Exception { decompress(file, true); } //文件解压缩 是否删除原始文件 public static void decompress(File file, boolean delete) throws Exception { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getPath().replace(".gz", "")); decompress(fis, fos); fis.close(); fos.flush(); fos.close(); if (delete) { file.delete(); } } //数据解压缩 public static void decompress(InputStream is, OutputStream os) throws Exception { GZIPInputStream gis = new GZIPInputStream(is); int count; byte data[] = new byte[1024]; while ((count = gis.read(data, 0, 1024)) != -1) { os.write(data, 0, count); } gis.close(); } //文件解压缩 ,默认删除源文件 public static void decompress(String path) throws Exception { decompress(path, true); } //文件解压缩 是否删除原始文件 public static void decompress(String path, boolean delete) throws Exception { File file = new File(path); decompress(file, delete); } }
阅读全文
0 0
- java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
- c#实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
- java实现zlib压缩解压缩:文件、byte[]字节数组,数据流
- java使用BZip算法压缩解压缩文件、数据流、byte[]字节数组
- java使用tar算法压缩解压缩文件、数据流、byte[]字节数组
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- JAVA字符串的GZIP压缩解压缩方法
- java实现压缩、解压缩文件
- java Gzip 压缩与解压缩
- java Gzip 压缩与解压缩
- linux文件的压缩解压缩
- JAVA 字符串压缩解压缩
- Java压缩,解压缩的实现
- Linux 文件压缩解压缩
- java 压缩解压缩文件
- java压缩、解压缩文件
- Java 压缩/解压缩 文件
- 前端之路随笔小记——属性:lang VS charset
- PAT 甲级 1091. Acute Stroke (30)
- 一点 · 线性表
- PAT 甲级 1093. Count PAT's (25)
- 前端的window.location
- java实现linux中gzip压缩解压缩算法:byte[]字节数组,文件,字符串,数据流的压缩解压缩
- linux内核驱动模块开发步骤及实例入门介绍
- 51 nod 苹果曼和树
- QT自定义控件之圆形头像的实现
- Codeforces Round #438 C Qualification Rounds(暴力+位运算)
- codevs 1026 逃跑的拉尔夫 BFS
- 3294: [Cqoi2011]放棋子
- C#导入Excel|读取Excel方法总结
- java 集合(Map, Set, List)