基于InputStreams的Zip文件

发布于 2024-12-03 18:47:14 字数 1710 浏览 0 评论 0原文

我有一种用 Java 压缩文件的方法:

public void compress(File[] inputFiles, OutputStream outputStream) {

    Validate.notNull(inputFiles, "Input files are required");
    Validate.notNull(outputStream, "Output stream is required");

    int BUFFER = 2048;

    BufferedInputStream origin = null;

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
    byte data[] = new byte[BUFFER];

    for (File f : inputFiles) {
        FileInputStream fi;
        try {
            fi = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Input file not found", e);
        }
        origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(f.getName());
        try {
            out.putNextEntry(entry);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int count;
        try {
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            origin.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    try {
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

如您所见,参数 inputFiles 是一个 File 对象数组。这一切都有效,但我希望使用 InputStream 对象的集合作为参数,以使其更加灵活。

但后来我遇到了一个问题,当创建一个新的 ZipEntry 时(如上面的代码所示),

ZipEntry entry = new ZipEntry(f.getName());

我没有文件名作为参数。

我应该如何解决这个问题?也许是一个带有(文件名,输入流)对的映射?

任何对此的想法表示赞赏!

谢谢, 内森

I have a method to zip files in Java:

public void compress(File[] inputFiles, OutputStream outputStream) {

    Validate.notNull(inputFiles, "Input files are required");
    Validate.notNull(outputStream, "Output stream is required");

    int BUFFER = 2048;

    BufferedInputStream origin = null;

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
    byte data[] = new byte[BUFFER];

    for (File f : inputFiles) {
        FileInputStream fi;
        try {
            fi = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Input file not found", e);
        }
        origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(f.getName());
        try {
            out.putNextEntry(entry);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int count;
        try {
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            origin.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    try {
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

As you can see parameter inputFiles is an Array of File objects. This all works, but I'd like to have instead a collection of InputStream objects as parameter to make it more flexible.

But then I have the problem that when making a new ZipEntry (as in code above)

ZipEntry entry = new ZipEntry(f.getName());

I don't have a filename to give as parameter.

How should i solve this? Maybe a Map with (fileName,inputStream) pairs?

Any thoughts on this are appreciated!

Thanks,
Nathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-12-10 18:47:14

我认为您的建议 Map 是一个很好的解决方案。

附注:完成后记得关闭输入流


如果您想让它更加“花哨”,您可以随时使用创建一个接口:

interface ZipOuputInterface {
    String getName();
    InputStream getInputStream();
}

并实现它在不同的情况下有所不同,例如文件:

class FileZipOutputInterface implements ZipOutputInterface {

    File file;

    public FileZipOutputInterface(File file) {
        this.file = file;
    }

    public String getName() {
        return file.getAbstractName();
    }
    public InputStream getInputStream() {
        return new FileInputStream(file);
    }
}

I think your suggestion Map<String, InputStream> is a good solution.

Just a side note: Remember to close the inputstreams after you are done


If you want to make it more "fancy" you can always use create an interface:

interface ZipOuputInterface {
    String getName();
    InputStream getInputStream();
}

And have it implemented differently in your different cases for instance File:

class FileZipOutputInterface implements ZipOutputInterface {

    File file;

    public FileZipOutputInterface(File file) {
        this.file = file;
    }

    public String getName() {
        return file.getAbstractName();
    }
    public InputStream getInputStream() {
        return new FileInputStream(file);
    }
}
冧九 2024-12-10 18:47:14

我觉得那张地图很好。如果您希望保留 ZIP 中文件的原始顺序,请注意您所使用的地图类型。在这种情况下使用 LinkedHashMap。

I think that map is good. Just pay attention on the type ofmap you are using if you wish to preserve the original order of files in your ZIP. In this case use LinkedHashMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文