开发者

Zip a file in BlackBerry Java app

Does anyone know how to zip a file using ZipOutputStream?

try {
    // Creating Zip Streams
    FileConnection path = (FileConnection) Connector.open(
            "file:///SDCard/BlackBerry/documents/" + "status.zip",
            Connector.READ_WRITE);

    if (!path.exists()) {
        path.create();
    }
    ZipOutputStream zinstream = new ZipOutputStream(
            path.openOutputStream());

    // Adding Entries
    FileConnection jsonfile = (FileConnection) Connector.open(
            "file:///SDCard/BlackBerry/documents/" + "st开发者_运维技巧atus.json",
            Connector.READ_WRITE);
    if (!jsonfile.exists()) {
        jsonfile.create();
    }
    int fileSize = (int) jsonfile.fileSize();
    if (fileSize > -1) {
        byte[] data = new byte[fileSize];
        InputStream input = jsonfile.openInputStream();
        input.read(data);

        ZipEntry entry = new ZipEntry(jsonfile.getName());
        zinstream.putNextEntry(entry);
        // zinstream.write(buf);
        // ZipEntry entry = null;

        path.setWritable(true);
        OutputStream out = path.openOutputStream();

        int len;
        while ((len = input.read(data)) != -1) {
            out.write(data, 0, len);
            out.flush();
            out.close();
            zinstream.close();
            content = "FILE EXIST" + entry;
        }

        jsonfile.close();
        path.close();
    }
} catch (...) {
    ...
}


The data should be written to the ZipOutputStream zinstream instead of to a new OutputStream out.

Its also important to close the ZipEntry entry after writing is done.

FileConnection path = (FileConnection) Connector.open(
        "file:///SDCard/BlackBerry/documents/" + "status.zip",
        Connector.READ_WRITE);

if (!path.exists()) {
    path.create();
}
ZipOutputStream zinstream = new ZipOutputStream(path.openOutputStream());

// Adding Entries
FileConnection jsonfile = (FileConnection) Connector.open(
        "file:///SDCard/BlackBerry/documents/" + "status.json",
        Connector.READ_WRITE);
if (!jsonfile.exists()) {
    jsonfile.create();
}
int fileSize = (int) jsonfile.fileSize();
if (fileSize > -1) {
    InputStream input = jsonfile.openInputStream();
    byte[] data = new byte[1024];

    ZipEntry entry = new ZipEntry(jsonfile.getName());
    zinstream.putNextEntry(entry);

    int len;
    while ((len = input.read(data)) > 0) {
        zinstream.write(data, 0, len);
    }
    zinstream.closeEntry();
}
jsonfile.close();
zinstream.close();
path.close();


BlackBerry uses the J2ME API which does not have all of the J2SE classes, such as the ZipOutputStream and ZipEntry and related classes. There are some classes such as ZLibOutputStream which may help, but that is just the byte-level compression and you'll end up having to implement the actual PKZIP container yourself (unless there is a third-party library out there that can do this for you).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜