ZipUtils
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.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
public static String zipName;
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile)
throws IOException {
if (zipFile.exists())
zipFile.delete();
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<File> 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 resFile 需要压缩的文件(夹)
* @param zipOut 压缩的目的文件
* @param rootpath 压缩的文件路径
* @throws FileNotFoundException 找不到文件时抛出
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFile(File resFile, ZipOutputStream zipOut,
String rootpath) throws IOException {
rootpath = rootpath
+ (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes(), StandardCharsets.UTF_8);
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
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();
// resFile.delete();
}
}
/**
* upZipFile:(解压缩文件)
* 将zipFile文件解压到folderPath目录下)
*
* @return int DOM对象
* @throws
*/
public static void upZipFile(File zipFile, String folderPath) throws IOException {
upZipFile(zipFile, folderPath, null);
}
/**
* upZipFile:(解压缩文件)
* 将zipFile文件解压到folderPath目录下)
*
* @return int DOM对象
* @throws
*/
public static void upZipFile(File zipFile, String folderPath, IAutelUnZipCallback iAutelUnZipCallback) throws IOException {
ZipFile zFile = new ZipFile(zipFile);
Enumeration zList = zFile.entries();
ZipEntry ze;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
String dirstr = folderPath + ze.getName();
dirstr = new String(dirstr.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
File f = new File(dirstr);
f.mkdir();
continue;
}
Log.d("upZipFile", "ze.getName() = " + ze.getName());
OutputStream os = new BufferedOutputStream(
new FileOutputStream(getRealFileName(folderPath, ze.getName())));
zipName = ze.getName();
InputStream is = new BufferedInputStream(zFile.getInputStream(ze));
int readLen;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zFile.close();
if (null != iAutelUnZipCallback)
iAutelUnZipCallback.unZipSuccess();
}
/**
* getRealFileName:(给定根目录,返回一个相对路径所对应的实际文件名)
*
* @param baseDir 指定根目录
* @param absFileName 相对路径名,来自于ZipEntry中的那么
* @return java.io.File 实际的文件
*/
private static File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/");
File ret = new File(baseDir);
ret.mkdirs();
String substr;
if (dirs.length > 0) {
for (int i = 0; i < dirs.length - 1; i++) {
substr = dirs[i];
ret = new File(ret, substr);
}
if (!ret.exists()) {
ret.mkdirs();
}
substr = dirs[dirs.length - 1];
ret = new File(ret, substr);
return ret;
}
return ret;
}
public interface IAutelUnZipCallback{
void unZipSuccess();
}
}