[英]decompressing a zip file… File not found exception
我从网上得到了以下代码,它看起来一切正常,但是我收到File not found exception ...
我在sdcard中有一个名为NewForestPonies.epub的文件
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
码:
String ZipFileLocation=Environment.getExternalStorageDirectory()+"/NewForestPonies.epub";
String unZipFileLocation=Environment.getExternalStorageDirectory()+"/DEST/";
Decompress decomp=new Decompress(ZipFileLocation, unZipFileLocation, "zip");
decomp.run();
public Decompress(String zipFile, String location,String t) {
super(t);
_zipFile = zipFile;
_location = location;
}
public void run() {
FileInputStream fin=null;
ZipInputStream zin=null;
File file =null;
ZipEntry ze ;
FileOutputStream fout=null;
try{
System.out.println(_zipFile );
System.out.println(_location);
fin = new FileInputStream(_zipFile);
zin = new ZipInputStream(fin);
ze= null;
byte[] buffer = new byte[1024];
int length;
while ((ze = zin.getNextEntry()) != null) {
file = new File((_location +"/" + ze.getName()));
file.getParentFile().mkdirs();
fout= new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
//MyDownloadListener.progress=70;
zin.close();
}catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
finally {
try {
fin.close();
zin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
错误:
03-20 15:49:15.909: ERROR/Decompress(9479): java.io.FileNotFoundException: /mnt/sdcard/DEST/NewForestPonies/iTunesMetadata.plist (Not a directory)
03-20 15:49:15.909: ERROR/Decompress(9479): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
03-20 15:49:15.909: ERROR/Decompress(9479): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
03-20 15:49:15.909: ERROR/Decompress(9479): at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
03-20 15:49:15.909: ERROR/Decompress(9479): at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
03-20 15:49:15.909: ERROR/Decompress(9479): at com.AndroidExplorer.Decompress.run(Decompress.java:42)
03-20 15:49:15.909: ERROR/Decompress(9479): at com.AndroidExplorer.DecompressActivity.onCreate(DecompressActivity.java:23)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.os.Looper.loop(Looper.java:132)
03-20 15:49:15.909: ERROR/Decompress(9479): at android.app.ActivityThread.main(ActivityThread.java:4028)
03-20 15:49:15.909: ERROR/Decompress(9479): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 15:49:15.909: ERROR/Decompress(9479): at java.lang.reflect.Method.invoke(Method.java:491)
03-20 15:49:15.909: ERROR/Decompress(9479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-20 15:49:15.909: ERROR/Decompress(9479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-20 15:49:15.909: ERROR/Decompress(9479): at dalvik.system.NativeStart.main(Native Method)
我认为您代码中的问题是您正在第1个while循环中关闭ZipInputStream zin 。
将以下代码用于run() ,可能会对您有所帮助。
public void run() {
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
File dest_file = new File(_location);
dest_file.mkdirs(); // creates if destination directory not existed
try {
fileInputStream = new FileInputStream(_zipFile);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = zipEntry.getName();
File file = new File(_location + zipEntryName);
if (file.exists()) {
} else if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
byte buffer[] = new byte[1024];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
int count;
while ((count = zipInputStream.read(buffer, 0, 1024)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
zipInputStream.close();
} catch (Exception e) {
Log.e("Decompress", "unzip", e);
}
}
您可以先尝试创建新文件吗?
file = new File((_location +"/" + ze.getName()));
file.getParentFile().mkdirs();
if (!file.isFile())
file.createNewFile();
...
我猜想要解压缩的ZIP文件位于/ mnt / sdcard / EPUB /,但是在您的代码中,您尝试访问的是可能位于ZIP文件的META-INF /目录中的container.xml。我没有该文件,因此大多是在这里猜测)。
因此,您应该做的是传递ZIP文件的位置(例如/mnt/sdcard/EPUB/book1.epub),如下所示:
Decompress("/mnt/sdcard/EPUB/book1.epub", "/mnt/sdcard/EPUB",t)
之后,您可以使用/mnt/sdcard/EPUB/META-INF/container.xml用自己的代码打开未压缩的container.xml。
检查以下项的返回值:
boolean result = file.getParentFile().mkdirs();
如果返回false
则不会创建目录。 这也许可以解释为什么您在异常中得到“ (不是目录) ”。
mkdirs()的文档说:
请注意,此方法在失败时不会引发IOException。 呼叫者必须检查返回值。
尝试像这样创建目录:
boolean result = (new File(_location, ze.getName())).getParentFile().mkdirs();
这样可以避免弄乱“ /”分隔符。
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
file = new File((_location, ze.getName()));
if (!file.exists())
file.mkdirs();
continue;
}
file = new File((_location +"/" + ze.getName()));
// file.getParentFile().mkdirs();
fout= new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
Exception表示它不是目录,因此在您的代码中-
while ((ze = zin.getNextEntry()) != null) {
file = new File((_location +"/" + ze.getName()));
**if(file.isDirectory())**
file.getParentFile().mkdirs();
fout= new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.