Java压缩和解压缩文件工具类,就是将文件压缩成“.zip”格式的文件,或者是将“.zip”文件解压,如何实现java的压缩与解压缩功能呢?代码如下:
package com.tpyyes;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.Deflater;
import java.util.zip.ZipException;
import org.apache.tools.zip.*;
public class UnZip{
/**
* 解压指定zip文件
*
* @param unZipfile
* 压缩文件的路径
* @param destFile
* 解压到的目录
*/
public static void unZip(String unZipfile, String destFile)
throws IOException, FileNotFoundException, ZipException {
BufferedInputStream bi;
ZipFile zipFile = new ZipFile(new File(unZipfile), "GBK");
@SuppressWarnings("rawtypes")
Enumeration e = zipFile.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = destFile + "/" + entryName;
if (ze2.isDirectory()) {
System.out.println("正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
System.out.println("正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile + "/" + entryName));
bi = new BufferedInputStream(zipFile.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zipFile.close();
}
/**
* 压缩文件
*
* @param srcFile
* 需要压缩的目录或者文件
* @param destFile
* 压缩文件的路径
*/
public static void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名
File zipFile = new File(srcFile);
try {
// 生成ZipOutputStream,会把压缩的内容全都通过这个输出流输出,最后写到压缩文件中去
ZipOutputStream zipOut = new ZipOutputStream(
new BufferedOutputStream(new FileOutputStream(destFile)));
// 设置压缩的注释
zipOut.setComment("comment");
// 设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码
zipOut.setEncoding("GBK");
// 启用压缩
zipOut.setMethod(ZipOutputStream.DEFLATED);
// 压缩级别为最强压缩,但时间要花得多一点
zipOut.setLevel(Deflater.BEST_COMPRESSION);
handleFile(zipFile, zipOut, "");
// 处理完成后关闭我们的输出流
zipOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 由doZip调用,递归完成目录文件读取
*
* @param zipFile
* @param zipOut
* @param dirName
* 这个主要是用来记录压缩文件的一个目录层次结构的
* @throws IOException
*/
private static void handleFile(File zipFile, ZipOutputStream zipOut, String dirName)
throws IOException {
System.out.println("遍历文件:" + zipFile.getName());
// 如果是一个目录,则遍历
if (zipFile.isDirectory()) {
File[] files = zipFile.listFiles();
if (files.length == 0) {// 如果目录为空,则单独创建之.
// 只是放入了空目录的名字
zipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()
+ File.separator));
zipOut.closeEntry();
} else {// 如果目录不为空,则进入递归,处理下一级文件
for (File file : files) {
// 进入递归,处理下一级的文件
handleFile(file, zipOut, dirName + zipFile.getName()
+ File.separator);
}
}
}
// 如果是文件,则直接压缩
else {
FileInputStream fileIn = new FileInputStream(zipFile);
// 放入一个ZipEntry
zipOut.putNextEntry(new ZipEntry(dirName + zipFile.getName()));
int length = 0;
byte[] buffer = new byte[1024];
// 放入压缩文件的流
while ((length = fileIn.read(buffer)) > 0) {
zipOut.write(buffer, 0, length);
}
// 关闭ZipEntry,完成一个文件的压缩
zipOut.closeEntry();
}
}
}上述Java压缩解压缩文件的工具类中的代码直接拿来使用即可!