在android中压缩特定文件夹的文件

时间:2016-12-08 06:54:45

标签: java android zip

我正在尝试压缩存储在外部存储中的“.jpg”文件,以便将该.zip文件发送到服务器。这是代码的片段。到目前为止,zip文件夹已创建,但内部没有文件。在使用winrar打开zip时,我收到“意外的归档结束”消息。

                            String[] s = new String[10];
                            ZipManager zipManager = new ZipManager();

                            File f = new File(inputPath + _report + "/");
                            File[] files = f.listFiles();
                            for (int i = 0; i < files.length; i++) {
                                File file = files[i];

                                String filePath = file.getPath();
                                if (filePath.endsWith(".jpg")) {

                                    tFilelist.add(filePath.substring(63, 98));
                                    s[a] = inputPath + _report + "/" + tFilelist.get(a);
                                    a++;
                                }
                            }

                            zipManager.zip(s, inputPath + _report + inputFile);

这是ZipManager类:

public class ZipManager {
    private static final int BUFFER = 80000;

    public void zip(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.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();
        }
    }


    private void dirChecker(String dir) {
        File f = new File(dir);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须关闭文件条目。在撰写内容后使用out.closeEntry();

        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); // open your file entry
            int count;

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