package com.zipUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
public static void zip(String src, String dest)throws IOException {
//提供了一个数据项压缩成一个ZIP归档输出流
ZipOutputStream out =null;
try {
File outFile= new File(dest);//源文件或者目录
FilefileOrDirectory = new File(src);//压缩文件路径
out = newZipOutputStream(new FileOutputStream(outFile));
//如果此文件是一个文件,否则为false。
if(fileOrDirectory.isFile()) {
zipFileOrDirectory(out,fileOrDirectory, "");
} else{
//返回一个文件或空阵列。
File[]entries = fileOrDirectory.listFiles();
for(int i = 0; i < entries.length; i++) {
//递归压缩,更新curPaths
zipFileOrDirectory(out,entries[i], "");
}
}
} catch (IOException ex){
ex.printStackTrace();
} finally {
//关闭输出流
if (out !=null) {
try{
out.close();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
private static voidzipFileOrDirectory(ZipOutputStream out,
FilefileOrDirectory, String curPath) throws IOException {
//从文件中读取字节的输入流
FileInputStream in =null;
try {
//如果此文件是一个目录,否则返回false。
if(!fileOrDirectory.isDirectory()) {
//压缩文件
byte[]buffer = new byte[4096];
intbytes_read;
in= new FileInputStream(fileOrDirectory);
//实例代表一个条目内的ZIP归档
ZipEntryentry = new ZipEntry(curPath
+fileOrDirectory.getName());
//条目的信息写入底层流
out.putNextEntry(entry);
while((bytes_read = in.read(buffer)) != -1) {
out.write(buffer,0, bytes_read);
}
out.closeEntry();
} else{
//压缩目录
File[]entries = fileOrDirectory.listFiles();
for(int i = 0; i < entries.length; i++) {
//递归压缩,更新curPaths
zipFileOrDirectory(out,entries[i], curPath
+fileOrDirectory.getName() + "/");
}
}
} catch (IOException ex){
ex.printStackTrace();
// throwex;
} finally {
if (in !=null) {
try{
in.close();
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
@SuppressWarnings("unchecked")
public static void unzip(String zipFileName,String outputDirectory)
throwsIOException {
ZipFile zipFile = null;
try {
zipFile = newZipFile(zipFileName);
Enumeration e= zipFile.entries();
ZipEntryzipEntry = null;
File dest =new File(outputDirectory);
dest.mkdirs();
while(e.hasMoreElements()) {
zipEntry= (ZipEntry) e.nextElement();
StringentryName = zipEntry.getName();
InputStreamin = null;
FileOutputStreamout = null;
try{
if(zipEntry.isDirectory()) {
Stringname = zipEntry.getName();
name= name.substring(0, name.length() - 1);
Filef = new File(outputDirectory + File.separator
+name);
f.mkdirs();
}else {
intindex = entryName.lastIndexOf("\");
if(index != -1) {
Filedf = new File(outputDirectory + File.separator
+entryName.substring(0, index));
df.mkdirs();
}
index= entryName.lastIndexOf("/");
if(index != -1) {
Filedf = new File(outputDirectory + File.separator
+entryName.substring(0, index));
df.mkdirs();
}
Filef = new File(outputDirectory + File.separator
+zipEntry.getName());
//f.createNewFile();
in= zipFile.getInputStream(zipEntry);
out= new FileOutputStream(f);
intc;
byte[]by = new byte[1024];
while((c = in.read(by)) != -1) {
out.write(by,0, c);
}
out.flush();
}
}catch (IOException ex) {
ex.printStackTrace();
thrownew IOException("解压失败:" + ex.toString());
}finally {
if(in != null) {
try{
in.close();
}catch (IOException ex) {
}
}
if(out != null) {
try{
out.close();
}catch (IOException ex) {
}
}
}
}
} catch (IOException ex){
ex.printStackTrace();
throw newIOException("解压失败:" + ex.toString());
} finally {
if (zipFile!= null) {
try{
zipFile.close();
}catch (IOException ex) {
}
}
}
}
}
Activity调用
packagecom.comc;
importjava.io.IOException;
importcom.zipUtil.ZipUtil;
importandroid.app.Activity;
import android.os.Bundle;
public class IZipActivityextends Activity {
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
ZipUtil.zip("/data/data/com.comc/databases","/data/data/com.comc/databases.zip");
ZipUtil.unzip("/data/data/com.comc/databases.zip","/data/data/com.comc/databases");
} catch (IOException e) {
e.printStackTrace();
}
}
}