现在很的app都有备份恢复功能。备份的时候都需要把文件打包,恢复的时候需要把文件解压。
压缩解压缩代码如下:
public class ZipUtils {
/**
* 解压缩功能. 将zipFile文件解压到folderPath目录下.
*
* @throws Exception
*/
public static int upZipFile(File zipFile, String folderPath) throws ZipException,
IOException {
// public static void upZipFile() throws Exception{
ZipFile zfile = new ZipFile(zipFile);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
String dirstr = folderPath + ze.getName();
// dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
File f = new File(dirstr);
f.mkdir();
continue;
}
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(folderPath, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
return 0;
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param baseDir
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
public static File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/");
File ret = new File(baseDir);
String substr = null;
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
substr = dirs[i];
try {
// substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret = new File(ret, substr);
}
if (!ret.exists())
ret.mkdirs();
substr = dirs[dirs.length - 1];
try {
// substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret = new File(ret, substr);
return ret;
}
return ret;
}
/**
* 压缩文件,文件夹
*
* @param srcFileString
* 要压缩的文件/文件夹名字
* @param zipFileString
* 指定压缩的目的和名字
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)
throws Exception {
// 创建Zip包
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
zipFileString));
// 打开要输出的文件
File file = new File(srcFileString);
// 压缩
ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
// 完成,关闭
outZip.finish();
outZip.close();
}// end of func
/**
* 压缩文件
*
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private static void ZipFiles(String folderString, String fileString,
ZipOutputStream zipOutputSteam) throws Exception {
if (zipOutputSteam == null)
return;
File file = new File(folderString + fileString);
// 判断是不是文件
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(fileString);
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
} else {
// 文件夹的方式,获取文件夹下的子文件
String fileList[] = file.list();
// 如果没有子文件, 则添加进去即可
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(fileString + File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
// 如果有子文件, 遍历子文件
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString, fileString + File.separator
+ fileList[i], zipOutputSteam);
}
}
}
}