Search code examples
androidzip

How to send a zip file in Android without saving it first?


I have this code to compress in zip format some files:

 public int zip(List<String> _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.size(); i++) {
                Log.v("Compress", "Adding: " + _files.get(i));
                FileInputStream fi = new FileInputStream(_files.get(i));
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files.get(i).substring(_files.get(i).lastIndexOf(File.separator) + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

I would like to get the ZipOutPutStream and send it through Intent or some other way, not saving it like in:

while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }

Is there a way to do it? Currently I just can send the zip using its saved name as a String.


Solution

  • There is no way. Intents are limited to 2 MB, so it could only hold a very small zipfile. And the receivers aren't expecting raw data. You also can't send objects (like a ZipOutputStream) because Intents only send the serialized version of its extras, as the receiver may not be in the same process.