简体   繁体   中英

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.

I need to insert an XML file into a zip archive programmatically with Java but without extracting the archive and recompressing it. 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. I get the message "archive.zip/file.txt is not a directory" when I attempt to access the archive in-place via:

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. 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/

[2] append a file to zip using TrueZip

[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. After I included this snippet below in my pom.xml, I could interact with ZIP files as virtual directories.

`   <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.

Maybe your class path is simply missing the TrueZIP Driver ZIP JAR?

I generally recommend to generate a sample project from the TrueZIP Archetype File* and then customize/integrate it to your needs. Please follow the instructions on the Getting Started page - it'll walk you through the process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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