0

I am developing an app that needs to frequently access a zip file to add, delete, and read files. I'm looking at using truezip because it promises that I can do all these fairly easily (being able to pass arond and read from a currently zipped file is the biggest advantage it presents) but in playing with it I am unable to add files to the archive. My code:

public void testfunction()
{
    //below create the archive if it doesn't exist
    TFile tFile = new TFile("src\\test\\resources\\archiveTest\\demoZip.zip");
    if (!tFile.exists()) // I get an EOFException here
    {
        tFile.createNewFile();
    }

    TFile innerFile = new TFile("src\\test\\resources\\archiveTest\\demoZip.zip\\someText.txt");
    innerFile.createNewFile(); // also here

    BufferedWriter out = new BufferedWriter(new FileWriter(innerFile));

    out.write("demo text");
    out.close(); // I know this is bad
}

When I run this I get a java.io.EOFException no matter how I try to arrange this simple thing. If I try to make sure that the file has been created already I just get the exception when I call tFile.exists() method. If I don't, then I get it when I try to create the innerFile later (even if the zip file already exists).

I should note: the examples in the truezip archetype use the TApplication class, but I can't do that. I have to be able to use this library pretty much out of the box. I cannot change the structure of the application, so making the application itself a TApplication subclass is not a viable solution (though I can alter the structure of the calling class however I need to).

At present my POM includes these dependencies (included from examples I found):

<dependency>
    <groupId>de.schlichtherle.truezip</groupId>
    <artifactId>truezip-file</artifactId>
    <version>7.4.1</version>
</dependency>
<dependency>
    <groupId>de.schlichtherle.truezip</groupId>
    <artifactId>truezip-driver-zip</artifactId>
    <version>7.4.1</version>
</dependency>
    <dependency>
    <groupId>de.schlichtherle.truezip</groupId>
    <artifactId>truezip-kernel</artifactId>
    <version>7.4.1</version>
</dependency>
<dependency>
    <groupId>de.schlichtherle.truezip</groupId>
    <artifactId>truezip-driver-file</artifactId>
    <version>7.4.1</version>
</dependency>

So I've no idea what I'm doing so any advice I could get would be appreciated as most examples on the internet seem to assume more knowledge than I have.

user7116
  • 63,008
  • 17
  • 141
  • 172
mja
  • 69
  • 1
  • 2
  • 9

1 Answers1

0

Your POM setup looks correct, so something like this should work:

public void testfunction()
{
    TFile innerFile = new TFile("src/test/resources/archiveTest/demoZip.zip/someText.txt");

    BufferedWriter out = new BufferedWriter(new FileWriter(innerFile));
    try {
        out.write("demo text");
    } finally {
        out.close();
    }
}

Mind that an archive file is a virtual directory, so in order to create one, you would call TFile.mkdir(), not TFile.createNewFile(). However, there is no need to create the archive file first. It will automatically get created if it doesn't already exist.

You can find more examples when using the Maven archetype of the project: http://truezip.java.net/kick-start/index.html

P.S.: TrueZIP 7.4.2 has been released - please update your dependencies.

Christian Schlichtherle
  • 3,125
  • 1
  • 23
  • 47