您的位置:首页 > 其它

手机中的Zip格式文件解压和文件夹的压缩

2012-12-16 16:20 344 查看
今天主要跟大家介绍下如何通过代码把手机存储中的zip文件解压到指定的地方以及如何对很多文件或者文件夹进行压缩生成zip格式的压缩文件。

1.将某个Zip文件解压到指定目录下:

/**
* 解压缩一个Zip格式的压缩文件
*
* @param zipFile 需要解压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException 当解压缩过程出错时抛出
*/
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();//mkdirs()无论是否父目录存在,都会创建目录成功!
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
//压缩文件里面的每一个文件或者文件夹都对应一个ZipEntry,通过zf.entries()函数可以把所有的文件或者文件夹对应的ZipEntry都读出来
ZipEntry entry = ((ZipEntry)entries.nextElement());

String str = folderPath + File.separator + entry.getName();
Log.i("zip", "file name = " + entry.getName());//打印出压缩文件中的所有文件或者文件夹名
File desFile = new File(str);

if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
if(entry.isDirectory()){//用来判断当前的ZipEntry对应的是文件还是文件夹
desFile.mkdir();//mkdir()就是创建一个目录,但是前提是创建的目录的父目录一定要存在
}else{
desFile.createNewFile();
InputStream in = zf.getInputStream(entry);
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[1024 * 1024];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}
}
}
现假如手机存储根目录下有个sample.zip,通过调用upZipFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample.zip"), Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "sample");就会将文件解压到根目录的sample文件夹中,打印的log如下:



2.将文件夹压缩成压缩文件并放到指定位置

/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), 1024 * 1024));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.close();
}

/**
* 压缩文件
*
* @param resFile 需要压缩的文件(夹)
* @param zipout 压缩的目的文件
* @param rootpath 压缩的文件路径
* @throws FileNotFoundException 找不到文件时抛出
* @throws IOException 当压缩过程出错时抛出
*/
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
throws FileNotFoundException, IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
if(!resFile.exists()){
return;
}
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
rootpath = rootpath + File.separatorChar;//添加File.separatorChar表示该ZipEntry对应的是文件夹,也就是此时调用ZipEntry.isDirectory()会返回true
zipout.putNextEntry(new ZipEntry(rootpath));
} else {
byte buffer[] = new byte[1024 * 1024];
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();
}
}
如果有什么问题或者不对的地方随时欢迎留言,谢谢。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: