2012-07-26 216 views
0

我有一個文件夾/mnt/sdcard/Bluetooth其中包含幾個zip文件。每個zip文件只包含一個文件。如何將這些zip文件的內容解壓縮到一個新文件中,其中包含每個zip文件的內容?這是我迄今所做的:從zip創建文件 - android

public class Main extends Activity { 

Object[] arrayOfRarFiles; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    String path = "/mnt/sdcard/Bluetooth"; 
    String nameOfFiles; 
    File folder = new File(path); 
    File[] listOfFiles = folder.listFiles(); 

    for (int i = 0; i < listOfFiles.length; i++) { 
     if (listOfFiles[i].isFile()) { 
      nameOfFiles = listOfFiles[i].getName(); 

      if (nameOfFiles.endsWith(".zip") 
        || nameOfFiles.endsWith(".ZIP")) { 

       try { 
        extractFile(nameOfFiles); 
       } catch (FileNotFoundException e) { 
        Log.e("EXTRACTFILE SAYS: ", e.getMessage()); 
       } 

      } 
     } 
    } 
} 

public void extractFile(String path) throws FileNotFoundException { 

    String zipFileName = "/mnt/sdcard/Bluetooth/" + path; 
    String extractedFileName = getApplicationContext().getFilesDir() 
      .getPath().toString() 
      + "Finger.FIR"; 

    ZipInputStream inStream = new ZipInputStream(new FileInputStream(
      zipFileName)); 
    OutputStream outStream = new FileOutputStream(extractedFileName); 





    Toast.makeText(getApplicationContext(), zipFileName, 
      Toast.LENGTH_SHORT).show(); 

} 

最後敬酒,在extractFile方法輸出每個zip文件的名稱。 zip文件夾內的文件是.FIR文件

回答

1

我認爲您可以使用以下函數,我在另一個SO question中找到。

請注意根據您的需要正確設置您的路徑和文件名參數。

public void extractFile(String path) throws FileNotFoundException { 

    String zipFileName = "/mnt/sdcard/Bluetooth/" + path; 
    String extractedFileName = getApplicationContext().getFilesDir() 
      .getPath().toString() 
      + "Finger.FIR"; 

    ZipInputStream inStream = new ZipInputStream(new FileInputStream(
      zipFileName)); 
    OutputStream outStream = new FileOutputStream(extractedFileName); 

    unpackZip(path ,zipFileName) 



    /*Toast.makeText(getApplicationContext(), zipFileName, 
      Toast.LENGTH_SHORT).show();*/ 

} 

private boolean unpackZip(String path, String zipname) 
{  
    InputStream is; 
    ZipInputStream zis; 
    try 
    { 
     String filename; 
     is = new FileInputStream(path + zipname); 
     zis = new ZipInputStream(new BufferedInputStream(is));   
     ZipEntry ze; 
     byte[] buffer = new byte[1024]; 
     int count; 

     while ((ze = zis.getNextEntry()) != null) 
     { 
      // zapis do souboru 
      filename = ze.getName(); 
      FileOutputStream fout = new FileOutputStream(path + filename); 

      // cteni zipu a zapis 
      while ((count = zis.read(buffer)) != -1) 
      { 
       fout.write(buffer, 0, count);    
      } 

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

     zis.close(); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 
+0

這樣做的工作,但我無法找到使用Astro文件管理器我的解壓縮文件。在代碼中,我應該把提取的文件放在哪裏? – 2012-07-26 13:21:09

+0

他們應該在他們解壓縮的路徑。你有沒有在那裏檢查? – 2012-07-26 13:47:47