1

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.

  • 1
    `I get exceptions` start with that. read those. google those. if you still can't make of those, add those to the question – njzk2 Jul 22 '15 at 18:48
  • Apologies, meant to include more, just really needed to get something typed out. I'm being told that "archive.zip\file.txt" is not a directory, which isn't very helpful for figuring out what is happening. – ShoeStringCollective Jul 22 '15 at 18:50
  • Please add the complete exception stack trace that you get from the code spinet that you posted. – Jason D Jul 22 '15 at 19:08
  • Added the trace. I know that that file exists within that zip file. My issue is that I'm attempting to access the inside of that zip file as described by truezip and not getting the same outcome. I imagine I'm not setting something up properly, but I don't have any good examples to go by. – ShoeStringCollective Jul 22 '15 at 19:54

1 Answers1

0

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.

Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47
  • Hi Christian, I appreciate the work put into your API. I had followed the Getting Started page originally. I am enhancing a pre-existing plugin, so starting from scratch with the archetype was not viable for me. I did solve my problem, which was a missing dependency in my pom.xml. See below: ` de.schlichtherle.truezip truezip-driver-zip 7.7.9 ` – ShoeStringCollective Jul 24 '15 at 13:57
  • I am running into an issue with a custom TArchiveDetector. If I import the JarDriver (de.schlichtherle.truezip.fs.archive.zip.JarDriver) required to make the custom TArchiveDetector, I get a null pointer exception during compilation. I think it must be a similar issue to not having the correct dependency again, but I haven't pinned down the problem yet. Thanks again for your help. – ShoeStringCollective Jul 24 '15 at 14:04
  • It's moved to https://christian-schlichtherle.bitbucket.io/truezip/kick-start/index.html . – Christian Schlichtherle Nov 16 '17 at 06:37