prakash
prakash

Reputation: 654

How to compress the files in Blackberry?

In my application I used html template and images for browser field and saved in the sdcard . Now I want to compress that html,image files and send to the PHP server. How can I compress that files and send to server? Provide me some samples that may help lot.

i tried this way... my code is

EDIT:

private void zipthefile() {
    String out_path = "file:///SDCard/" + "newtemplate.zip";
    String in_path = "file:///SDCard/" + "newtemplate.html";
    InputStream inputStream = null;
    GZIPOutputStream os = null;
    try {
        FileConnection fileConnection = (FileConnection) Connector
                .open(in_path);//read the file from path
        if (fileConnection.exists()) {
            inputStream = fileConnection.openInputStream();
        }

        byte[] buffer = new byte[1024];

        FileConnection path = (FileConnection) Connector
                .open(out_path,
                        Connector.READ_WRITE);//create the out put file path

        if (!path.exists()) {
            path.create();
        }
        os = new GZIPOutputStream(path.openOutputStream());// for create the gzip file

        int c;

        while ((c = inputStream.read()) != -1) {
            os.write(c);
        }
    } catch (Exception e) {
        Dialog.alert("" + e.toString());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                Dialog.alert("" + e.toString());
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
                Dialog.alert("" + e.toString());
            }
        }
    }

}

this code working fine for single file but i want to compress all the file(more the one file)in the folder .

Upvotes: 1

Views: 550

Answers (1)

Mister Smith
Mister Smith

Reputation: 28168

In case you are not familiar with them, I can tell you that in Java the stream classes follow the Decorator Pattern. These are meant to be piped to other streams to perform additional tasks. For instance, a FileOutputStream allows you to write bytes to a file, if you decorate it with a BufferedOutputStream then you get also buffering (big chunks of data are stored in RAM before being finally written to disc). Or if you decorate it with a GZIPOutputStream then you get also compression.

Example:

//To read compressed file:
InputStream is = new GZIPInputStream(new FileInputStream("full_compressed_file_path_here"));

//To write to a compressed file:
OutputStream os = new GZIPOutputStream(new FileOutputStream("full_compressed_file_path_here"));

This is a good tutorial covering basic I/O . Despite being written for JavaSE, you'll find it useful since most things work the same in BlackBerry.

In the API you have these classes available:
GZIPInputStream
GZIPOutputStream
ZLibInputStream
ZLibOutputStream

If you need to convert between streams and byte array use IOUtilities class or ByteArrayOutputStream and ByteArrayInputStream.

Upvotes: 2

Related Questions