0

我有一个/mnt/sdcard/Bluetooth包含多个zip文件的文件夹。每个 zip 文件只包含一个文件。如何将每个 zip 文件的内容提取到包含每个 zip 文件内容的新文件中?这是我到目前为止所做的:

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

}

最后一个 toast,在extractFile方法中输出每个 zip 文件的名称。zip文件夹中的文件是.FIR文件

4

1 回答 1

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;
}
于 2012-07-26T13:11:16.720 回答