在Java中压缩时如何维护文件夹结构?

时间:2012-06-23 21:32:21

标签: java android zip

我想压缩一个像这样结构的文件夹:

温度/ folder1中/ file1的

温度/文件夹2 / file2的

温度/ file3的

并完全维护目录结构。 目前,当我拉链时,我得到一个不保持目录结构的zip。看起来像这样 文件1 文件2 file3的

如何将文件添加到各自的文件夹中,就像所有普通的压缩应用程序一样?

这是我到目前为止的代码:

package com.damastah.deflash;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

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

    private ArrayList<File> _files;
    private String _zipFile;

    public Compress(ArrayList<File> files, String zipFile) {
        _files = files;
        _zipFile = zipFile;
    }

    public void zip() {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(_zipFile);

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

            byte data[] = new byte[BUFFER];
            Log.e("Compress - zip", "test");
            for (int i = 0; i < _files.size(); i++) {
                Log.v("Compress", "Adding: " + _files.get(i).getAbsolutePath());
                FileInputStream fi = new FileInputStream(_files.get(i)
                        .getAbsolutePath());
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry;
                if (_files.get(i).getAbsolutePath().contains("."))
                    entry = new ZipEntry(_files
                            .get(i)
                            .getAbsolutePath()
                            .substring(
                                    _files.get(i).getAbsolutePath()
                                            .lastIndexOf("/") + 1));
                else
                    entry = new ZipEntry(_files.get(i).getAbsolutePath());
                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 个答案:

答案 0 :(得分:1)

我不是android开发人员,所以请将此代码视为仅提示

class ZipCompress {
    public static void main(String[] args) throws IOException {

        String dir = "d:\\My folder\\";
        // this entries I want to put in archives in the way they are now
        String[] entries = { "temp\\folder1\\file1.txt", "temp\\folder2\\file2.txt",
                "temp\\file3.txt" };

        ZipOutputStream zipos = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(dir + "archive.zip")));

        // time to create entries and fill them with data
        for (String entrie : entries) {
            System.out.println("Writing file: " + dir + entrie);

            // prepering file stream
            BufferedInputStream fileStream = new BufferedInputStream(
                    new FileInputStream(dir + entrie));

//--------------------------------------------------------------------------
//          Here we decide how we entry will look like in archive.
//          We use only part of path from String[] entries
//          like "temp\\folder1\\file1.txt"
//--------------------------------------------------------------------------
            ZipEntry newEntry = new ZipEntry(entrie);

            newEntry.setComment("comment to entry: " + newEntry);

            // now lets put this entry in archive
            zipos.putNextEntry(newEntry);

            // lets put data from file to current archive entry
            int c;
            while ((c = fileStream.read()) != -1)
                zipos.write(c);
            fileStream.close();
        }
        zipos.close();
    }
}

修改

我不确定你想做什么

if (_files.get(i).getAbsolutePath().contains("."))
                    entry = new ZipEntry(_files
                            .get(i)
                            .getAbsolutePath()
                            .substring(
                                    _files.get(i).getAbsolutePath()
                                            .lastIndexOf("/") + 1));

从我看到它删除文件的路径只留下文件名(类似_files.get(i).getName())。如果这是真的那么这就是你的zip文件中没有文件夹结构的原因。你说zip条目应该只是那个文件名,没有任何文件夹。

因此,如果您希望zip文件包含路径的某些部分 像/my/full/path/to/folder/temp/folder1/file1这样的temp/folder1/file1只是在您创建ZipEntry时删除该路径中不必要的部分

String dir = "/my/full/path/to/folder/";
for (int i = 0; i < _files.size(); i++) {
...
    if (_files.get(i).getAbsolutePath().contains(".")) {
        entry = new ZipEntry(_files
            .get(i).getAbsolutePath().replace(dir, ""));
...