使用Truezip将新文件添加到zip存档

时间:2015-07-22 18:45:40

标签: java truezip

我进行了搜索和搜索,所以当我说我真的难过时,请相信我。注意:我被迫使用Java 6。

我需要使用Java以编程方式将XML文件插入到zip存档中,但不提取存档并重新压缩它。我被指向Truezip作为我的问题的解决方案,但我似乎无法让Truezip认识到我的" archive.zip"是一个档案。我收到消息" archive.zip/file.txt不是目录"当我尝试通过以下方式就地访问存档时

new TFile("example.zip/audit.xml") 

我已经引用了Truezip博客以及StackOverflow上的一些问题,但我找不到任何代码示例来说明我尝试做的事情。任何人都可以提供/指向执行这样简单任务的片段吗?

完整堆栈跟踪:

java.io.FileNotFoundException: /Users/ss042306/java-scan-example/target/fortify/example.zip/audit.xml
at de.schlichtherle.truezip.file.TFileOutputStream.newOutputStream(TFileOutputStream.java:147)
at de.schlichtherle.truezip.file.TFileOutputStream.<init>(TFileOutputStream.java:116)
at com.fortify.ps.maven.plugin.sca.ScanMojo.copyPreviousComments(ScanMojo.java:745)
at com.fortify.ps.maven.plugin.sca.ScanMojo.execute(ScanMojo.java:332)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:317)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:152)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.nio.file.NoSuchFileException: /Users/ss042306/java-scan-example/target/fortify/example.zip/audit.xml
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
    at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
    at java.nio.file.Files.newOutputStream(Files.java:216)
    at de.schlichtherle.truezip.fs.nio.file.FileOutputSocket$1OutputStream.<init>(FileOutputSocket.java:243)
    at de.schlichtherle.truezip.fs.nio.file.FileOutputSocket.newOutputStream(FileOutputSocket.java:257)
    at de.schlichtherle.truezip.file.TFileOutputStream.newOutputStream(TFileOutputStream.java:143)
    ... 24 more

我引用了以下资源:

[1] http://illegalexception.schlichtherle.de/2011/07/26/appending-to-zip-files/

[2] append a file to zip using TrueZip

[3] https://truezip.java.net/kick-start/tutorial.html

编辑:Truezip没有按照描述运行,因为我没有将.zip文件作为虚拟目录处理所需的所有依赖项。在我将这个片段包含在我的pom.xml中之后,我可以将ZIP文件作为虚拟目录进行交互。

`   <dependency>
        <groupId>de.schlichtherle.truezip</groupId>
        <artifactId>truezip-driver-zip</artifactId>
        <version>7.7.9</version>
    </dependency>` 

没有这种依赖关系,就没有TArchiveDetector可用于识别&#34; .zip&#34;作为档案。

1 个答案:

答案 0 :(得分:0)

也许您的类路径错过了TrueZIP Driver ZIP JAR?

我通常建议从TrueZIP Archetype File *生成一个示例项目,然后根据您的需要自定义/集成它。请按照Getting Started页面上的说明操作 - 它将引导您完成整个过程。