Java.util.zip添加新文件会覆盖整个jar? [英] Java.util.zip adding a new file overwrites entire jar?

查看:1204
本文介绍了Java.util.zip添加新文件会覆盖整个jar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java.util.zip将一些配置资源添加到jar文件中。
当我调用addFileToZip()方法,它完全覆盖jar,而不是将文件添加到jar。为什么我需要写配置到jar是完全不相关的。并且我不想使用任何外部API。



编辑:jar不是在VM中运行,org.cfg.resource是我试图的包保存文件,文件是标准文本文档,正在编辑的jar包含在使用此方法之前的正确信息。



我的代码:

  public void addFileToZip(文件fileToAdd,文件zipFile)
{
ZipOutputStream zos = null;
FileInputStream fis = null;
ZipEntry ze = null;
byte [] buffer = null;
int len;

try {
zos = new ZipOutputStream(new FileOutputStream(zipFile));
} catch(FileNotFoundException e){
}

ze = new ZipEntry(org+ File.separator +cfg+
File.separator + resource+ File.separator + fileToAdd.getName());
try {
zos.putNextEntry(ze);

fis = new FileInputStream(fileToAdd);
buffer = new byte [(int)fileToAdd.length()];

while((len = fis.read(buffer))> 0)
{
zos.write(buffer,0,len);
}
} catch(IOException e){
}
try {
zos.flush();
zos.close();
fis.close();
} catch(IOException e){
}
}


解决方案

您显示的代码覆盖一个文件,无论它是否是一个zip文件。 ZipOutputStream 不关心现有数据。



我会推荐


  1. 使用 ZipInputStream 创建新文件。


  2. c $ c>


  3. 将现有条目复制到新文件。


  4. 添加新条目。

  5. p>






希望在Java 7中有 Zip文件系统,这将为您节省大量工作。



我们可以直接写入zip文件中的文件

  Map< String,String> ; env = new HashMap<>(); 
env.put(create,true);
路径path = Paths.get(test.zip);
URI uri = URI.create(jar:+ path.toUri());
try(FileSystem fs = FileSystems.newFileSystem(uri,env))
{
路径nf = fs.getPath(new.txt);
try(Writer writer = Files.newBufferedWriter(nf,StandardCharsets.UTF_8,StandardOpenOption.CREATE)){
writer.write(hello);
}
}


I am using java.util.zip to add some configuration resources into a jar file. when I call addFileToZip() method it overwrites the jar completely, instead of adding the file to the jar. Why I need to write the config to the jar is completely irrelevant. and I do not wish to use any external API's.

EDIT: The jar is not running in the VM and org.cfg.resource is the package I'm trying to save the file to, the file is a standard text document and the jar being edited contains the proper information before this method is used.

My code:

public void addFileToZip(File fileToAdd, File zipFile)
{
    ZipOutputStream zos = null;
    FileInputStream fis = null;
    ZipEntry ze = null;
    byte[] buffer = null;
    int len;

    try {
        zos = new ZipOutputStream(new FileOutputStream(zipFile));
    } catch (FileNotFoundException e) {
    }

    ze = new ZipEntry("org" + File.separator + "cfg" + 
            File.separator + "resource" + File.separator + fileToAdd.getName());
    try {
        zos.putNextEntry(ze);

        fis = new FileInputStream(fileToAdd);
        buffer = new byte[(int) fileToAdd.length()];

        while((len = fis.read(buffer)) > 0)
        {
            zos.write(buffer, 0, len);
        }           
    } catch (IOException e) {
    }
    try {
        zos.flush();
        zos.close();
        fis.close();
    } catch (IOException e) {
    }
}

解决方案

The code you showed overrides a file no matter if it would be a zip file or not. ZipOutputStream does not care about existing data. Neither any stream oriented API does.

I would recommend

  1. Create new file using ZipOutputStream.

  2. Open existing with ZipInputStream

  3. Copy existing entries to new file.

  4. Add new entries.

  5. Replace old file with a new one.


Hopefully in Java 7 we got Zip File System that will save you a lot of work.

We can directly write to files inside zip files

Map<String, String> env = new HashMap<>(); 
env.put("create", "true");
Path path = Paths.get("test.zip");
URI uri = URI.create("jar:" + path.toUri());
try (FileSystem fs = FileSystems.newFileSystem(uri, env))
{
    Path nf = fs.getPath("new.txt");
    try (Writer writer = Files.newBufferedWriter(nf, StandardCharsets.UTF_8, StandardOpenOption.CREATE)) {
        writer.write("hello");
    }
}

这篇关于Java.util.zip添加新文件会覆盖整个jar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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