Creating a zip file with some files on sdcard

When I posted the question a few days ago, I realized that the stock e-mail application cannot send multiple files in the application: https://stackoverflow.com/questions/5773006/sending-email-with-multiple-attachement-fail-with- default-email-android-app-but

Unfortunately, I did not receive an answer to it, so you need to find a workaround.

The user must select some pdf files from the list and send them by e-mail using the stock application. As the multiple attachment is denied, I will create a zip file with all the requested files and send this unique file.

So, is it possible to create an archive with some files on the SDCard?

Currently, I have found the following: http://developer.android.com/reference/java/util/zip/ZipFile.html

public ZipFile (file)

C: API Level 1 Creates a new ZipFile with the specified file.

But I do not understand how to use this with multiple files.

Many thanks,

+5
source share
2 answers

ZipFileis a shortcut for the case of a single file. If you want to make several files, you need to work with ZipOutputStream- just one click from the javadoc page that you specified.

And that j avadoc also has an example on how to encrypt multiple files.

+2
source

, :

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zipper {
    private static final int BUFFER = 2048;

    public static void zip(String[] files, String zipFile) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFile);

            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

            byte data[] = new byte[BUFFER];

            for (int i = 0; i < files.length; i++) {
                Log.v("Compress", "Adding: " + files[i]);
                FileInputStream fi = new FileInputStream(files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 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();
        }
    }
}
+1

All Articles