/*
 * [TestTrueZIP7.java]
 *
 * Summary: Example use of TrueZIP 7..
 *
 * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2011-05-13 initial version
 */
package com.mindprod.example;

import de.schlichtherle.truezip.file.TArchiveDetector;
import de.schlichtherle.truezip.file.TFile;

import java.io.EOFException;
import java.io.IOException;

import static java.lang.System.err;
import static java.lang.System.out;

/**
 * Example use of TrueZIP 7.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2011-05-13 initial version
 * @since 2011-05-13
 */
public final class TestTrueZIP7
    {
    // --------------------------- main() method ---------------------------

    /**
     * Example use of TrueZIP
     * <p/>
     * You must first download the truezip-7.jar, from https://truezip.dev.java.net
     * prune out the ant and sample classes and put it in your ext directories.
     * <p/>
     * Run with parms C:\temp\temp.zip  E:\dir\somefile.html
     * where temp.zip does not exist but E:\dir\somefile.html does.
     * Or where temp.zip is a TrueZIP compatible ZIP file.
     *
     * @param args name of a TrueZIP-compatible ZIP file to be created(or existing), name of a file to add to the ZIP file.
     *
     * @throws java.io.IOException on I/O failure.
     */
    public static void main( String[] args ) throws IOException
        {
        // Arrange for ZIP extension files to be treated as virtual directories.
        final TFile zipFile = new TFile( args[ 0 ], new TArchiveDetector( "zip" ) );
        if ( !zipFile.exists() )
            {
            if ( !zipFile.mkdirs() )
                {
                err.println( "mkdirs failed" );
                }
            }
        if ( !zipFile.isArchive() )
            {
            err.println( "First file must be TrueZip compatible zip" );
            }
        if ( !zipFile.isDirectory() )
            {
            err.println( "Archive not being treated as virtual zip." );
            }
        // ensure toAdd file is never treated as a virtual directory, even if it has a .zip extension.
        final TFile toAdd = new TFile( args[ 1 ], TArchiveDetector.NULL );
        if ( !toAdd.exists() )
            {
            err.println( args[ 1 ] + " file does not exist." );
            }
        // create slot in the archive to copy into; cannot copy directly into root of archive
        TFile zipEntry = new TFile( zipFile, toAdd.getName(), TArchiveDetector.NULL );
        out.println( "about to copy " + toAdd.getCanOrAbsPath() + " to zipentry: " + zipEntry.getCanOrAbsPath() );
        // add a file to the zip, preserving file date.
        try
            {
            toAdd.cp_p( zipEntry );
            }
        catch ( EOFException e )
            {
            err.println( "add to zip failed" );
            }
        // Look at contents of zip.
        // Cast needed since listFiles nominally returns java.io.File[] but actually returns de.schlichtherle.io.TFile[]
        final TFile[] contents = ( TFile[] ) zipFile.listFiles();
        out.println( "contents of zip:" );
        for ( TFile content : contents )
            {
            out.println( content.getCanOrAbsPath() );
            }
        // do the physical i/o to build the zip and close.
        TFile.umount();
        }
    }