简体   繁体   English

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

[英]Adding new files to a zip archive with Truezip

I've searched and searched, so trust me when I say I'm truly stumped. 我已经搜寻了很多东西,所以当我说我真的很沮丧时,请相信我。 Note: I'm forced to use Java 6. 注意:我被迫使用Java 6。

I need to insert an XML file into a zip archive programmatically with Java but without extracting the archive and recompressing it. 我需要使用Java以编程方式将XML文件插入zip存档中,但不提取存档并重新压缩它。 I was pointed towards Truezip as a solution to my problem but I cannot seem to get Truezip to recognize that my "archive.zip" is an archive. 有人将Truezip指向我的问题的解决方案,但是我似乎无法让Truezip认识到我的“ archive.zip”是一个档案。 I get the message "archive.zip/file.txt is not a directory" when I attempt to access the archive in-place via: 当我尝试通过以下方式就地访问存档时,收到消息“ archive.zip/file.txt不是目录”:

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

I've referenced the Truezip blog as well as a few questions on StackOverflow but I can't find any sample of code that illustrates what I'm attempting to do. 我已经引用了Truezip博客以及有关StackOverflow的一些问题,但是我找不到任何能说明我要做什么的代码示例。 Can anyone provide/point to a snippet that performs a simple task like that? 任何人都可以提供/指向执行此类简单任务的代码段吗?

Full Stack trace: 全栈跟踪:

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

I've referenced the following resources: 我引用了以下资源:

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

[2] append a file to zip using TrueZip [2] 使用TrueZip将文件追加到zip

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

EDIT: Truezip wasn't functioning as described because I did not have all the dependencies needed for it to treat .zip files as virtual directories. 编辑:Truezip不能如描述的那样工作,因为我没有将.zip文件视为虚拟目录所需的所有依赖项。 After I included this snippet below in my pom.xml, I could interact with ZIP files as virtual directories. 在将以下代码段包含在pom.xml中之后,我就可以将ZIP文件作为虚拟目录进行交互了。

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

Without this dependency, there was no TArchiveDetector available to identify ".zip" as an archive. 没有这种依赖性,就没有TArchiveDetector可用于将“ .zip”标识为存档。

Maybe your class path is simply missing the TrueZIP Driver ZIP JAR? 也许您的课程路径只是缺少TrueZIP驱动程序ZIP JAR?

I generally recommend to generate a sample project from the TrueZIP Archetype File* and then customize/integrate it to your needs. 我通常建议从TrueZIP原型文件*生成示例项目,然后根据需要对其进行自定义/集成。 Please follow the instructions on the Getting Started page - it'll walk you through the process. 请按照“ 入门”页面上的说明进行操作-它将引导您完成整个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM