2012-03-20 59 views
1

我必須使用哪個庫來處理android上的檔案(如rar,zip)。一些示例代碼。我找不到任何存檔文件的例子。Android RAR,zip庫

+0

這已被問過[這裏RAR(http://stackoverflow.com/questions/561107/rar-archives-with-java)和[這裏是zip](http://stackoverflow.com/questions/9558595/android-library-zip)。請嘗試先搜索。 – RivieraKid 2012-03-20 09:41:11

+0

可能使用[RAR archives with java]重複(https://stackoverflow.com/questions/561107/rar-archives-with-java) – 2017-08-30 00:03:35

回答

2

您是壓縮還是解壓縮? (或兩者?) 以前我用過ZipInputStream。 您使用的策略可能取決於Zip存儲位置(在SD卡上/在APK資源中/在APK擴展中)。 例如,如果它是一項資產,您可以使用AssetManager將文件作爲InputSteam打開。 如果它在SD卡上,則可能需要使用ZipFile

這裏有一個Java教程,可以幫助: http://java.sun.com/developer/technicalArticles/Programming/compression/

+0

我想使用存檔文件而不解壓縮到SDcard。我有文件存檔,我想與這些文件工作。 – Lang 2012-03-20 09:54:16

+0

那麼你是否正在調查內容而沒有提取任何東西? – HaemEternal 2012-03-20 09:57:55

+0

是的。並使用這個文件 – Lang 2012-03-20 10:04:21

1

看到這個例子

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()); 
         System.out.println("^^^^^^UnzippingFile^"+ze.getName()); 
         ///code to search is given string exists or not in a Sentence 
         String haystack = ze.getName(); 
         String needle1 = ".DS_Store"; 
         int index1 = haystack.indexOf(needle1); 
         if (index1 != -1) 
         { 
          System.out.println("The string contains the substring " 
+ needle1); 
          continue; 
         } 
         /*else 
          System.out.println("The string does not contain the 
substring " + needle1);*/ 


         if(ze.isDirectory()) { 
         _dirChecker(ze.getName()); 
         } else { 
         FileOutputStream fout = new FileOutputStream(_location + 
ze.getName()); 
         // replace for loop with: 
         byte[] buffer = new byte[1024]; 
         int length; 
         while ((length = zin.read(buffer))>0) { 
         fout.write(buffer, 0, length); 
         } 

         zin.closeEntry(); 
         fout.close(); 
         } 




        }////Outer While 
        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(); 
        } 
       } 
-4

右鍵點擊你的項目--->屬性 - - > Java Build path ----> Libraries ---> Add External Jar 然後以任何格式在您的項目中添加庫.zip或.rar