Java 将文件附加到 zip 文件中 [英] Java appending files into a zip

查看:37
本文介绍了Java 将文件附加到 zip 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
使用 Java 将文件附加到 zip 文件

我有一个 zip 文件,其中包含一些文件夹,但重要的是 dir,里面是另一个名为 folder 的文件夹,文件夹包含许多我需要更新的文件.

I have a zip that contains a few folders in it but the important one is dir and inside that is another folder called folder and folder contains a lot of files that I need to be able to update.

我现在在 zip 之外有一个名为 dir 的目录,其中包含我需要更新的文件的文件夹,因此路径相同.我如何将这些文件更新到 zip 中?

I have now a dir outside of the zip called dir and in that is folder with the files i need to update in so the paths are the same. How can i update those files into the zip?

棘手的部分是 dir 位于 zip 的根目录,它包含很多文件夹,而不仅仅是文件夹,但我只需要更新文件夹中的文件,我不能弄乱文件夹外的任何文件但仍在目录中.

The tricky part is that dir is at the root of the zip and it contains a lot of folders not just folder but i only need to update the files in folder i can't mess with any of the files out side of folders but still in dir.

这能做到吗?我知道这可以在 bash 中使用 -u 修饰符来完成,但如果可能的话,我更愿意用 java 来完成.

Can this be done? I know this can be done in bash using the -u modifier but I would prefer to do this with java if it's possible.

感谢您对此问题的任何帮助

Thank you for any help with this issue

为了更清楚

拉链内/dir/folder/filestoupdate

Inside Zip /dir/folder/filestoupdate

拉链外/dir/folder/filestomoveintozip

Outside the zip /dir/folder/filestomoveintozip

推荐答案

好吧,这里是最后一个方法,它与我之前粘贴的方法相同,我实际上是从 @Qwe 之前发布的链接中的 stackoverflow 主题中获得的,但我添加了路径变量,以便它可以将文件添加到 zip 内的文件夹

Alright well here is the final method it's the same method i pastebinned before which i actually got from the stackoverflow topic in the link @Qwe posted before but i added the path variable so that it could add files to folders inside the zip

好的,那么现在如何在我上面的示例中使用它我想将一个文件添加到另一个文件夹内的文件夹中,我会在这样的问题中使用我的设置来做到这一点

Alright so now how to use it in my example above i wanted to add a file into a folder that was inside another folder i would do that using my setup in the question like this

private void addFilesToZip(File source, File[] files, String path){
    try{
        File tmpZip = File.createTempFile(source.getName(), null);
        tmpZip.delete();
        if(!source.renameTo(tmpZip)){
            throw new Exception("Could not make temp file (" + source.getName() + ")");
        }
        byte[] buffer = new byte[4096];
        ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
        for(int i = 0; i < files.length; i++){
            InputStream in = new FileInputStream(files[i]);
            out.putNextEntry(new ZipEntry(path + files[i].getName()));
            for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
                out.write(buffer, 0, read);
            }
            out.closeEntry();
            in.close();
        }
        for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){
            if(!zipEntryMatch(ze.getName(), files, path)){
                out.putNextEntry(ze);
                for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){
                    out.write(buffer, 0, read);
                }
                out.closeEntry();
            }
        }
        out.close();
        tmpZip.delete();
    }catch(Exception e){
        e.printStackTrace();
    }
}

private boolean zipEntryMatch(String zeName, File[] files, String path){
    for(int i = 0; i < files.length; i++){
        if((path + files[i].getName()).equals(zeName)){
            return true;
        }
    }
    return false;
}

感谢链接最终能够稍微改进该方法,以便它可以添加不在根目录中的文件,现在我是一个快乐的露营者:)希望这也能帮助其他人

Thanks for the link ended up being able to improve that method a bit so that it could add in files that weren't in the root and now i'm a happy camper :) hope this helps someone else out as well

编辑我在这个方法上做了更多的工作,这样它不仅可以附加到 zip 文件,还可以更新 zip 文件中的文件

EDIT I worked a bit more on the method so that it could not only append to the zip but it also is able to update files within the zip

使用这样的方法

File[] files = {new File("/path/to/file/to/update/in")};
addFilesToZip(new File("/path/to/zip"), files, "folder/dir/");

您不会以/开始路径(最后一个变量),因为它不是在 zip 条目中列出的方式

You wouldn't start the path (last variable) with / as that's not how it's listed in the zip entries

这篇关于Java 将文件附加到 zip 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆