2

I've written an app using PhoneGap/Cordova that downloads content to the local file system for later viewing, it does this using the Cordova libraries for access in the file system and transferring files.

Some of the data comes in the form of a zip archive, so I need to unzip the content.

There isn't a standard plugin for extracting ZIP files in Cordova, but I took the existing plugin for iOS that we were already using, kept the same interface, and reimplemented in Java wrapping ZipME for the BB platform.

My problem is that then I try to access my file to unzip it, I get a "File not found" exception being thrown.

This is the code:

public void extract(String sourceFile, String targetDirectory) throws IOException {

    byte[] buff = new byte[1024];

    InputStream inStream = null;
    OutputStream outStream = null;

    try {
        FileConnection fcIn = (FileConnection) Connector.open(sourceFile, Connector.READ_WRITE);
        FileConnection fcOut;

        inStream = fcIn.openInputStream();
        ZipInputStream zipIS = new ZipInputStream(inStream);
        ZipEntry zipEntry = zipIS.getNextEntry();

        while (zipEntry != null) {
            String fileName = zipEntry.getName();
            fcOut = (FileConnection) Connector.open(targetDirectory + fileName, Connector.READ_WRITE);
            outStream = fcOut.openOutputStream();

            int readLength;
            while ((readLength = inStream.read(buff)) > 0) {
                outStream.write(buff, 0, readLength);
            }
            outStream.flush();
            outStream.close();

            zipEntry = zipIS.getNextEntry();
        }

    } catch (RuntimeException e) {
        Logger.error(TAG + "Unzip failed: " + e.getMessage());
        throw new ZipException("Unable to unzip file: " + e.getMessage());

    } finally {
        // Cleanup code
        if (inStream != null) {
            inStream.close();
        }
        if (outStream != null) {
            outStream.close();
        }
    }
}

Debugging confirms that I am passing in the following parameters:

Source file: file:///SDCard/downloadable/B3/B3.zip Dest Dir: file:///SDCard/downloadable/B3/

And examining the SD card on a test device, and looking at the synthetic SD card on a simulator both confirm the following file structure:

root
  - BlackBerry
      - Etc
  - temp
  - downloadable
      - B1
      - B2
          - B2.zip
      - B3
          - B3.zip
      - B4
      - B5
      - B6

I'm running the tests on BB 7.0 and 7.1, Cordova is V2.3.0, and the test devices are a 9800, 9810, and simulators for 9800 and 9810 running either the BB 7.0 or 7.1 OS.

Chris Cooper
  • 4,982
  • 1
  • 17
  • 27

0 Answers0