Search code examples
javatruezip

How to use TrueZip to compress files?


I have a file, let's say C:\source.dat. I want to compress it into a zip file C:\folder\destination.zip.

I can't find a straightforward example, and the HelloWorld provided with the Maven project doesn't really apply in my case because I'm not writing a plaintext file, so hopefully someone can enlighten me on this.

For reference, the code provided in the example:

@Override
protected int work(String[] args) throws IOException {
    // By default, ZIP files use character set IBM437 to encode entry names
    // whereas JAR files use UTF-8.
    // This can be changed by configuring the respective archive driver,
    // see Javadoc for TApplication.setup().
    final Writer writer = new TFileWriter(
            new TFile("archive.zip/dir/HälloWörld.txt"));
    try {
        writer.write("Hello world!\n");
    } finally {
        writer.close();
    }
    return 0;
}

Solution

  • It's as simple as this:

    new TFile("source.dat").cp(new TFile("destination.zip/source.dat"));
    

    For more information, please refer to the Javadoc for the TFile class at http://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TFile.html .

    You may also want to try the TrueZIP Archetype File*, which is introduced at http://truezip.java.net/kick-start/index.html . The archetype generates many sample programs which you should explore to get a feel for the API.