Java Util Zip - 归档中缺少的文件(Android)

时间:2015-04-29 09:01:19

标签: java android zip ziparchive archiving

在我的应用中,用户选择的图像被打包到一个zip存档中。问题是存档中缺少许多文件(假设我选择30张图像,存档只包含12或17,可能会有所不同)。

我是否必须增加一些缓冲区值,或者为什么只有部分文件存档而其余文件丢失?

感谢。

我用来压缩的代码:

public void zip(String[] files, String zipFile) throws IOException {
        File zipDirectory = new File(Environment.getExternalStorageDirectory()
                + "/laborator/");
        if (!zipDirectory.exists()) {
            zipDirectory.mkdirs();
        } else {
            System.out.println("folder already exists!");
        }

        BufferedInputStream origin = null;
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(Environment.getExternalStorageDirectory()
                        + "/laborator/" + 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();
            System.out.println("ziping done");
            sendZip();
        }
    }

我传递给zip方法的路径列表:

> 04-29 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/Pictures/Messenger/received_10203971818075142.jpeg
> 04-29 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190814.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190810.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190759.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190756.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190754.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190745.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190742.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190730.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190724.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190722.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190717.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190713.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_190709.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150428_182706.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195510.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195506.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195504.jpg 04-29
> 11:55:44.450: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195438.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195059.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195057.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195055.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195012.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195008.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195007.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_195005.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_194905.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_194845_HDR.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_194836_HDR.jpg 04-29
> 11:55:44.455: I/System.out(17558):
> /storage/emulated/0/DCIM/Camera/20150426_194733.jpg

0 个答案:

没有答案