嘿,我正在尝试使用此类解压缩文件:
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;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
Main.java中的代码
String zipFile = Environment.getExternalStorageDirectory() + "/tmp.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/tmp/";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
问题出在这一行: while((ze = zin.getNextEntry())!= null){
zin.getNextEntry()始终为null!谁能告诉我如何解决这个问题?感谢。
答案 0 :(得分:0)
咦,
你正在使用我的确切解压缩类,我的工作正常。
您是否100%确定您的zipFile变量正在访问实际文件?您是否确保将android.permission.WRITE_EXTERNAL_STORAGE添加到清单中并且SDCard未安装到您的计算机上?
如果是这样,您可以尝试使用zipFile路径创建File对象并运行file.exists()以确保一切都按原样设置。祝你好运!