【发布时间】:2017-09-28 14:52:01
【问题描述】:
我正在将 zip 文件从服务器下载到内部存储。下载后,我想将该文件解压缩到内部存储中。我的文件已下载到内部存储,我可以看到,但解压缩时我无法读取它。下面是我的 unzipfile() 方法。谁能告诉我哪里出错了?
public void unzipfile()
{
try {
Log.d("unzipiing","unzipping");
ContextWrapper cw = new ContextWrapper(context);
String name_="foldername"; //Folder name in device android/data/
File directory = cw.getDir(name_, Context.MODE_PRIVATE);
File mypath=new File(directory,"dwnld");
FileOutputStream fout = new FileOutputStream(mypath);
File yourFile = new File(directory,"dwnld.zip");
Log.d("unzipiing","filepath -" + yourFile.getPath());
FileInputStream fin = new FileInputStream(yourFile.getPath());
ZipInputStream zin = new ZipInputStream(fin);
Log.d("unzipiing","zin size -" + zin.available());
// zin.available() give -1 in console log
BufferedInputStream in = new BufferedInputStream(zin);
BufferedOutputStream out = new BufferedOutputStream(fout);
byte b[] = new byte[zin.available()];
int n;
Log.d("unzip","n - " + in.read(b,0,1024));
while ((n = in.read(b,0,1024)) >= 0) {
out.write(b,0,n);
Log.d("unzip byte"," - " + n);
}
out.flush();
out.close();
in.close();
fin.close();
zin.close();
}
catch (Exception e){
}
}
【问题讨论】:
-
问题到底出在哪里还不清楚。您可以尝试其他解决方案来完成它:stackoverflow.com/questions/3382996/…
-
感谢您的链接和帮助。将尝试该解决方案。但我想知道为什么这段代码不起作用:-(
-
您可以暂时评论您的代码,继续使用下一组解决方案,当您有空时尝试调试您的代码。
-
您应该在多个地方使用 File#exists() 检查文件或目录是否存在。此外,catch 块中没有代码,因此您不知道会发生什么。谁下载了文件?走哪条路?告诉我们所有完整路径。
标签: android storage unzip internal