【发布时间】:2011-09-08 20:11:17
【问题描述】:
我正在使用通过 google 找到的类来解压缩 .zip 文件。.zip 包含文件和文件夹。问题是FileOutputStreamthrowsFileNotFoundException..但是该文件应该从.zip文件中获取,那么它以前怎么存在?
这是我在AsyncTask 中使用的代码:
@Override
protected Boolean doInBackground(String... params) {
try {
String zipFile = Path + FileName;
FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(Path
+ ze.getName()); // <-- crashes here
while ((length = zin.read(buffer)) > 0) {
fout.write(buffer, 0, length);
publishProgress(length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch (Exception e) {
mProgressDialog.dismiss();
return false;
}
return true;
}
另一个下载.zip的AsyncTask:
@Override
protected Boolean doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
File f = new File(Path+FileName);
if(!f.exists())
{
f.mkdirs();
if(!f.createNewFile())
{
f.delete();
f.createNewFile();
}
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Path+FileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
e.getCause();
return false;
}
我再次收到带有(是目录)消息错误的FileNotFoundExcpetion!。
【问题讨论】:
-
Path的内容是什么?会不会是缺少尾随
/?