我有一个不错的java代码,解压缩.zip文件。但这个代码的问题是Java:解压缩文件,不创建目标文件夹
- 我需要在运行此代码之前创建目标文件夹(注意:只有文件夹不是文件)。
- 否则我会得到路径未找到异常。
因此,如果以前不知道压缩文件内容,此代码将无法正常工作。所以我认为这是无用的代码。任何人有更好的逻辑?或需要编辑Bellow代码?
package com.mireader;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author jon
*/
public class Decompress {
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] buffer = new byte[1024];
int length;
int i=0;
while ((ze = zin.getNextEntry()) != null) {
Log.v("t", ze.toString());
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
Log.i("my","Comes to if");
_dirChecker(ze.getName());
}
else {
Log.i("my","Comes to else");
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
Log.i("My tag","Success");
}catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
Log.i("mytag", "Creating new folder");
f.mkdirs();
System.out.print("stp:"+f.getName());
}
}
}
*您是否试过*您的代码? – bigstones 2011-03-02 11:50:32
这不是他的代码 - 快速谷歌揭示了这一点:http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29也许他应该写自己的代码,或开发能够知道,如果一段代码做什么他期待。看来他不能做任何事情! – 2011-03-02 11:57:56