一. Gzip解压缩
工具类:
package com.mazaiting;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GzipUtil {
/**utf-8编码*/
public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
/**iso-8859-1编码*/
public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";
/**
* "utf-8"编码压缩
* @param string 要压缩的字符串
* @return 字节数组
*/
public static byte[] compress(String string) {
return compress(string, GZIP_ENCODE_UTF_8);
}
/**
* 指定编码压缩
* @param string 要压缩的字符串
* @param encoding 编码
* @return 字节数组
*/
public static byte[] compress(String string, String encoding) {
// 判断是否为空
if (null == string || string.length() == 0) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream;
try {
gzipOutputStream = new GZIPOutputStream(baos);
gzipOutputStream.write(string.getBytes(encoding));
gzipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return baos.toByteArray();
}
/**
* 解压为字节数组
* @param bytes 要解压的字节数组
* @return 字节数组
*/
public static byte[] uncompress(byte[] bytes) {
if (null == bytes || bytes.length == 0) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
GZIPInputStream unGzip;
try {
unGzip = new GZIPInputStream(bais);
byte[] buffer = new byte[256];
int n;
while ((n = unGzip.read(buffer)) >= 0) {
baos.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return baos.toByteArray();
}
/**
* "utf-8"编码解压
* @param bytes 要解压的字节数组
* @return 字节数组
*/
public static String uncompressToString(byte[] bytes) {
return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
}
/**
* 指定编码解压
* @param string 要压缩的字符串
* @param encoding 编码
* @return 字节数组
*/
public static String uncompressToString(byte[] bytes, String encoding) {
if (null == bytes || bytes.length == 0) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
GZIPInputStream unGzip;
try {
unGzip = new GZIPInputStream(bais);
byte[] buffer = new byte[256];
int n;
while ((n = unGzip.read(buffer)) >= 0) {
baos.write(buffer, 0, n);
}
return baos.toString(encoding);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
具体使用:
package com.mazaiting;
public class Test {
public static void main(String[] args) {
String string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println("字符串长度: " + string.length());
System.out.println("压缩后: " + GzipUtil.compress(string).length);
System.out.println("解压后: " + GzipUtil.uncompress(GzipUtil.compress(string)).length);
System.out.println("解压后字符串长度: " + GzipUtil.uncompressToString(GzipUtil.compress(string)).length());
}
}
二、Zip解压缩
文中所需工具类Java 文件遍历
- 工具类
/**
* Zip格式数据操作类
* @author mazaiting
*/
public class ZipUtil {
/**缓冲字节--1M*/
private static final int BUFF_SIZE = 1024 * 1024;
/**
* 批量压缩文件(文件夹)
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection resFileList, File zipFile) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(
new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipOut, "");
}
zipOut.close();
}
/**
* 批量压缩文件(文件夹)
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection resFileList, File zipFile, String comment) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(
new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipOut, "");
}
zipOut.setComment(comment);
zipOut.close();
}
/**
* 解压缩一个文件
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException
* @throws ZipException
*/
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
// 根据路径创建一个文件
File desDir = new File(folderPath);
// 判断文件是否存在,如果不存在则创建
if (!desDir.exists()) {
desDir.mkdirs();
}
// 创建一个压缩文件
ZipFile zFile = new ZipFile(zipFile);
// 循环遍历
for (Enumeration> entries = zFile.entries(); entries.hasMoreElements(); ){
ZipEntry entry = (ZipEntry) entries.nextElement();
InputStream in = zFile.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
// 判断文件是否存在
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
// 判断父文件夹是否存在
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
// 创建新文件
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte[] buffer = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) >0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}
/**
* 解压文件名包含传入文字的文件
* @param zipFile 压缩文件
* @param folderPath 目标文件夹
* @param nameContains 传入的文件匹配名
* @throws ZipException 压缩格式有误时抛出
* @throws IOException IO错误时抛出
*/
public static ArrayList upZipSelectedFile(File zipFile, String folderPath,
String nameContains) throws ZipException, IOException {
ArrayList fileList = new ArrayList();
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdir();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
if (entry.getName().contains(nameContains)) {
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
// str.getBytes("GB2312"),"8859_1" 输出
// str.getBytes("8859_1"),"GB2312" 输入
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
return fileList;
}
/**
* 压缩文件
* @param resFile 需要压缩的文件(夹)
* @param zipOut 压缩的目的文件
* @param rootPath 压缩的文件路径
* @throws IOException
*/
private static void zipFile(File resFile, ZipOutputStream zipOut, String rootPath) throws IOException {
// 判断文件路径长度是否大于0, 大于0时string为"/", 等于0时为""
String string = rootPath.trim().length() == 0 ? "" : File.separator;
// 压缩文件生成的路径
rootPath = rootPath + string + resFile.getName();
// 路径转码
rootPath = new String(rootPath.getBytes("8859_1"), "GB2312");
// 判断压缩的是否是路径
if (resFile.isDirectory()) {
// 获取当前路径下的所有文件
File[] listFiles = resFile.listFiles();
for (File file : listFiles) {
zipFile(file, zipOut, rootPath);
}
} else {
byte[] buffer = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(resFile), BUFF_SIZE);
zipOut.putNextEntry(new ZipEntry(rootPath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipOut.write(buffer, 0, realLength);
}
in.close();
zipOut.flush();
zipOut.closeEntry();
}
}
/**
* 获得压缩文件内文件列表
* @param zipFile 压缩文件
* @return 压缩文件内文件名称
* @throws IOException
* @throws ZipException
*/
public static ArrayList getEntriesNames(File zipFile) throws ZipException, IOException {
ArrayList entryNames = new ArrayList<>();
Enumeration> entries = getEntriesEnumeration(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
entryNames.add(new String(
getEntryName(entry).getBytes("GB2312"), "8859_1"));
}
return entryNames;
}
/**
* 获得压缩文件对象的名称
* @param entry 压缩文件对象
* @return 压缩文件对象的名称
* @throws UnsupportedEncodingException
*/
private static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
return new String(entry.getName().getBytes("GB2312"), "8859_1");
}
/**
* 取得压缩文件对象的注释
* @param entry 压缩文件对象
* @return 压缩文件对象的注释
* @throws UnsupportedEncodingException
*/
public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
return new String(entry.getComment().getBytes("GB2312"), "8859_1");
}
/**
* 获得压缩文件内压缩文件对象以取得其属性
* @param zipFile 压缩文件
* @return 返回一个压缩文件列表
* @throws IOException
* @throws ZipException
*/
private static Enumeration> getEntriesEnumeration(File zipFile) throws ZipException, IOException {
ZipFile zf = new ZipFile(zipFile);
return zf.entries();
}
}
- 使用
public class Test {
public static void main(String[] args) throws IOException {
ArrayList listFiles = DirTraversal.listFiles("E:\\web");
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
}
File zipFile = new File("E:\\web\\file.zip");
ZipUtil.zipFiles(listFiles, zipFile);
// File zipFile = new File("E:\\web\\file.zip");
// ZipUtil.zipFiles(listFiles, zipFile, "Hello");
// File zipFile = new File("E:\\web\\file.zip");
// ZipUtil.upZipFile(zipFile, "E:\\web");
}
}
三、Apache Commons Compress解压缩
支持ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200, bzip2, 7z, arj, lzma, snappy, DEFLATE, lz4, Brotli and Z files格式。
/**
* BZip2解压缩工具类
* @author mazaiting
*/
public class BZip2Util {
/**缓冲字节*/
public static final int BUFFER = 1024;
/**后缀名*/
public static final String EXT = ".bz2";
/**
* 数据压缩
* @param data 数据字节
* @return
* @throws IOException
*/
public static byte[] compress(byte[] data) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 压缩
compress(bais, baos);
byte[] output = baos.toByteArray();
// 从缓冲区刷新数据
baos.flush();
// 关闭流
baos.close();
bais.close();
return output;
}
/**
* 文件压缩
* @param file 文件
* @param delete 是否删除原文件
* @throws IOException
*/
public static void compress(File file, boolean delete) throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
compress(fis, fos);
fos.flush();
fos.close();
fis.close();
if (delete) {
file.delete();
}
}
/**
* 数据压缩
* @param is 输入流
* @param os 输出流
* @throws IOException
*/
private static void compress(InputStream is, OutputStream os) throws IOException {
BZip2CompressorOutputStream bcos = new BZip2CompressorOutputStream(os);
int count;
byte data[] = new byte[BUFFER];
while((count = is.read(data, 0, BUFFER)) != -1){
bcos.write(data, 0, count);
}
bcos.finish();
bcos.flush();
bcos.close();
}
/**
* 文件压缩
* @param path 文件路径
* @param delete 是否删除原文件
* @throws IOException
*/
public static void compress(String path, boolean delete) throws IOException{
File file = new File(path);
compress(file, delete);
}
/**
* 数据解压缩
* @param data 数据
* @return
* @throws IOException
*/
public static byte[] deCompress(byte[] data) throws IOException{
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 解压缩
deCompress(bais, baos);
data = baos.toByteArray();
baos.flush();
baos.close();
bais.close();
return data;
}
/**
* 文件解压缩
* @param file 文件
* @param delete 是否删除源文件
* @throws IOException
*/
public static void deCompress(File file, boolean delete) throws IOException{
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT, ""));
deCompress(fis, fos);
fos.flush();
fos.close();
fis.close();
if (delete) {
file.delete();
}
}
/**
* 解压缩
* @param is 输入流
* @param os 输出流
* @throws IOException
*/
private static void deCompress(InputStream is, OutputStream os) throws IOException {
BZip2CompressorInputStream bcis = new BZip2CompressorInputStream(is);
int count;
byte data[] = new byte[BUFFER];
while((count = bcis.read(data, 0, BUFFER)) != -1){
os.write(data, 0, count);
}
bcis.close();
}
/**
* 文件解压缩
* @param path 文件路径
* @param delete 是否删除源文件
* @throws IOException
*/
public static void deCompress(String path, boolean delete) throws IOException{
File file = new File(path);
deCompress(file, delete);
}
}
上面为BZip2解压缩代码, 若是想用其他格式的解压缩,可以将代码中的BZip2CompressorInputStream 和BZip2CompressorOutputStream相对应的替换就可以。
- .ar对应ArArchiveInputStream和ArArchiveOutputStream
- .cpio对应CpioArchiveInputStream和CpioArchiveOutputStream
- .dump对应DumpArchiveInputStream
- .tar对应TarArchiveInputStream和TarArchiveOutputStream
- .zip对应ZipArchiveInputStream和ZipArchiveOutputStream
- .gzip对应GzipCompressorInputStream和GzipCompressorOutputStream
- .xz对应XZCompressorInputStream和XZCompressorOutputStream
- .pack200对应Pack200CompressorInputStream和Pack200CompressorOutputStream
- .bzip2对应BZip2CompressorInputStream和BZip2CompressorOutputStream
- .7z对应SevenZFile和SevenZOutputFile
- .arj对应ArjArchiveInputStream
- .lzma对应LZMACompressorInputStream和LZMACompressorOutputStream
- .snappy对应SnappyCompressorInputStream和SnappyCompressorOutputStream
- .deflate对应DeflateCompressorInputStream和DeflateCompressorOutputStream
- .lz4对应BlockLZ4CompressorInputStream和BlockLZ4CompressorOutputStream
- .brotli 对应BrotliCompressorInputStream
- .z对应ZCompressorInputStream