2012-03-13 44 views
10

tôi đã sau mã từ net và có vẻ mọi thứ đều đúng nhưng tôi nhận được không tìm thấy tập tin ngoại lệ ...giải nén một file zip ... File not found ngoại lệ

Tôi có một tập tin gọi là NewForestPonies. epub trong sdcard

Permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Mã sản phẩm:

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(); 
      } 




    } 

} 

errror:

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) 
+0

kiểm tra xem bạn có file trong đường dẫn này /mnt/sdcard/EPUB/META-INF/container.xml –

+0

@Samir i chỉ có zip filee .. tôi có cần tạo thư mục đích và tập tin theo cách thủ công không? của nó được thực hiện trong mã phải không? – vnshetty

+0

Bạn có quyền viết trên thẻ sd không? – Jerome

Trả lời

4

Tôi nghĩ vấn đề trong mã của bạn là bạn đang đóng ZipInputStreamzin trong 1 vòng lặp while.

Sử dụng mã bên dưới cho chạy(), nó có thể giúp bạn.

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); 
    } 
} 
0

Tôi đoán rằng các tập tin ZIP bạn muốn giải nén nằm ở/mnt/sdcard/EPUB /, nhưng trong mã của bạn bạn đang cố gắng truy cập vào container.xml đó có lẽ là thường trú trong thư mục META-INF/của tệp ZIP (Tôi không có tệp, vì vậy phần lớn nó được đoán ở đây).

Vì vậy, những gì bạn cần làm là vượt qua vị trí của tập tin ZIP (ví dụ /mnt/sdcard/EPUB/book1.epub), như thế này:

Decompress("/mnt/sdcard/EPUB/book1.epub", "/mnt/sdcard/EPUB",t) 

Sau đó bạn có thể mở container nén. xml với mã của riêng bạn tại /mnt/sdcard/EPUB/META-INF/container.xml

+0

giả định của bạn là đúng và tôi sẽ thử mã của bạn ... – vnshetty

+0

không hoạt động..do tôi cần tạo thư mục đích và tệp theo cách thủ công? được thực hiện thông qua mã phải không? – vnshetty

+0

@vnshetty Ok, sau khi tải về một tập tin EPUB ngẫu nhiên & chạy mã, tôi thấy vấn đề là thế này: \t \t 'fout = FileOutputStream mới (_location + ze.getName()); ' \t \t nơi không có seperator chèn vào giữa hai String, do đó, mặc dù thư mục chính xác được tạo ra, FileOutputStream được hướng dẫn để ghi vào một tập tin trong một thư mục không tồn tại. Hãy thử đoạn mã sau và nó sẽ hoạt động: 'file = new File (_location, ze.getName());' 'file.getParentFile(). Mkdirs(); ' ' fout = new FileOutputStream (file); ' Btw, nó luôn an toàn hơn khi sử dụng File (String, String) mới để xây dựng filepath. – Kai

2

Bạn có thể thử tạo tệp mới trước không?

file = new File((_location +"/" + ze.getName())); 
file.getParentFile().mkdirs(); 
if (!file.isFile()) 
    file.createNewFile(); 
... 
0

Kiểm tra giá trị trả về:

boolean result = file.getParentFile().mkdirs(); 

Nếu nó trở false các thư mục sẽ không được tạo ra. Điều này có thể giải thích tại sao bạn nhận được "(Không phải thư mục)" trong trường hợp ngoại lệ.

Các tài liệu của mkdirs() nói:

Lưu ý rằng phương pháp này không ném IOException trên thất bại. Người gọi phải kiểm tra giá trị trả lại.

Cố gắng tạo ra các thư mục như thế này:

boolean result = (new File(_location, ze.getName())).getParentFile().mkdirs(); 

này tránh can thiệp vào '/' ký tự phân cách.

0
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();   
    } 
0

ngoại lệ cho thấy nó không phải là một thư mục, vì vậy trong số- bạn

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();   
     } 
Các vấn đề liên quan