Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Create files from zip - android

I have a folder /mnt/sdcard/Bluetooth which contains several zip files. Each of this zip-files contains only one file. How can I extract the content for each of these zip-files into a new file, containing the content of each zip-file? This is what I've done so far:

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

}

The last toast, in the extractFile method outputs the name of each zip-file. The file inside the zip-folders are .FIR files

Upvotes: 0

Views: 340

Answers (1)

Korhan Ozturk
Korhan Ozturk

Reputation: 11320

I think you can use the following function which I've found in another SO question.

Pay attention to set your path and file name parameters correctly based on your need.

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

Upvotes: 1

Related Questions