Zip in CN1 - Sample required

59 views
Skip to first unread message

romanca...@gmail.com

unread,
May 25, 2017, 3:16:55 PM5/25/17
to CodenameOne Discussions
Hi! im looking for an example of how to implement the unzip functionality in CN1.

i download a zip file and i want to uncompress it in some directory.

can anybody helps?


thanks!


If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

Shai Almog

unread,
May 26, 2017, 12:31:41 AM5/26/17
to CodenameOne Discussions, romanca...@gmail.com
Hi,
did you look at this cn1lib: https://github.com/codenameone/ZipSupport/

romanca...@gmail.com

unread,
May 26, 2017, 7:52:53 AM5/26/17
to CodenameOne Discussions, romanca...@gmail.com
yes! but no code samples about any functionalityes. or at least i didnt find it.

can you help with a sample to decompress a zip who already download from the server?

thanks!

Shai Almog

unread,
May 27, 2017, 1:36:13 AM5/27/17
to CodenameOne Discussions, romanca...@gmail.com
I only wrote some code to compress a zip not decompress. It's in the skin designer project.

romanca...@gmail.com

unread,
May 27, 2017, 2:15:12 PM5/27/17
to CodenameOne Discussions, romanca...@gmail.com
Shai! here is my contribution for a future zip/unzip class


import com.codename1.io.FileSystemStorage;
import com.codename1.io.Log;
import com.codename1.io.Storage;
import com.codename1.ui.Display;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import net.sf.zipme.ZipEntry;

import net.sf.zipme.ZipInputStream;

/**
 *
 * @author roman
 */
public class Unzip {

    public Unzip(String zipName, String dirDest) {

        InputStream is;
        try {
            is = Storage.getInstance().createInputStream(zipName);
            ZipInputStream zipStream = new ZipInputStream(is);
            ZipEntry entry;

            // create a buffer to improve copy performance later.
            byte[] buffer = new byte[2048];

            while ((entry = zipStream.getNextEntry()) != null) {
                String s = entry.getName();
//            System.out.println(s);

                // Once we get the entry from the stream, the stream is
                // positioned read to read the raw data, and we keep
                // reading until read returns 0 or less.
                String outdir = FileSystemStorage.getInstance().getAppHomePath();
                if (outdir.length() > 0)
                {
                    outdir = outdir + "/" + dirDest;
                }
                String outpath = outdir + "/" + entry.getName();
                OutputStream output = null;
                try {
                    output = FileSystemStorage.getInstance().openOutputStream(outpath);
                    int len = 0;
                    while ((len = zipStream.read(buffer)) > 0) {
                        output.write(buffer, 0, len);
                    }
                } finally {
                    // we must always close the output file
                    if (output != null) {
                        output.close();
                    }
                }
            }
        } catch (IOException ex) {
            Log.p(ex.getMessage(), 0);
        }
    }
/**
 * Unzip from stream
 * @param zipFile
 * @param dirDest
 */
    public Unzip(InputStream zipFile, String dirDest) {

        InputStream is;
        try {
            is = zipFile;
            ZipInputStream zipStream = new ZipInputStream(is);
            ZipEntry entry;

            // create a buffer to improve copy performance later.
            byte[] buffer = new byte[2048];

            while ((entry = zipStream.getNextEntry()) != null) {
                String s = entry.getName();
                String outdir = FileSystemStorage.getInstance().getAppHomePath();
                if (outdir.length() > 0)
                {
                    outdir = outdir + "/" + dirDest;
                }
                String outpath = outdir + "/" + entry.getName();
                OutputStream output = null;
                try {
                    output = FileSystemStorage.getInstance().openOutputStream(outpath);
                    int len = 0;
                    while ((len = zipStream.read(buffer)) > 0) {
                        output.write(buffer, 0, len);
                    }
                } finally {
                    // we must always close the output file
                    if (output != null) {
                        output.close();
                    }
                }
            }
        } catch (IOException ex) {
            Log.p(ex.getMessage(), 0);

Shai Almog

unread,
May 28, 2017, 12:02:21 AM5/28/17
to CodenameOne Discussions, romanca...@gmail.com
Go to the github page of the zip project. Press fork.
Press the edit button on the README.md file and add some docs/samples. Commit the change then click the submit pull request button to send us a pull request with your edit.
Reply all
Reply to author
Forward
0 new messages