Android:将图片导出为zip

时间:2014-08-02 15:01:49

标签: android zip

有人可以帮助我,在我的应用程序中我想将我的图像从图库导出到.zip,但我不知道我需要做什么。我将不胜感激任何提示

2 个答案:

答案 0 :(得分:0)

使用“ Easy Unrar,Unzip&压缩 “

找到图库的目录,压缩它

<强> EDITED

试试这个:

public static void compress(String[] folder, String compFile) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compFile)));
    try { 
        byte data[] = new byte[BUFFER_SIZE];

        for (int i = 0; i < folder.length; i++) {
            FileInputStream fi = new FileInputStream(folder[i]);    
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(folder[i].substring(folder[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            }
            finally {
                origin.close();
            }
        }
    }
    finally {
        out.close();
    }
}

答案 1 :(得分:0)

这是一些基本的邮政编码。基本上创建一个字符串数组,其中包含要包含在zip中的所有图像文件路径以及另一个用于zip文件目标路径的字符串。将这些传递给方法。

public static void zip(String[] files, String zipFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try { 
    byte data[] = new byte[BUFFER_SIZE];

    for (int i = 0; i < files.length; i++) {
        FileInputStream fi = new FileInputStream(files[i]);    
        origin = new BufferedInputStream(fi, BUFFER_SIZE);
        try {
            ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                out.write(data, 0, count);
            }
        }
        finally {
            origin.close();
        }
    }
}
finally {
    out.close();
}

}

或者,您可以使用Zip4j库。

快乐的编码! :d