/*
 * @(#)TestTrueZip.java
 *
 * Summary: Example use of TrueZip.
 *
 * Copyright: (c) 2009-2010 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: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2009-05-14 - initial version
 */
package com.mindprod.example;

import de.schlichtherle.io.ArchiveDetector;
import de.schlichtherle.io.DefaultArchiveDetector;
import de.schlichtherle.io.File;

import java.io.IOException;

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

/**
 * Example use of TrueZip.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-05-14 - initial version
 * @since 2009-05-14
 */
public final class TestTrueZip
    {
    // --------------------------- main() method ---------------------------

    /**
     * Example use of TrueZip
     * <p/>
     * You must first download the truezip jar from https://truezip.dev.java.net and put it in your ext directories.
     * <p/>
     * Run with parms C:\temp\temp.zip and E:\dir\somefile.html
     * where temp.zip does not exist but E:\dir\somefile.html does.
     * Or where temp.dir 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
        {
        // "File" here is de.schlichtherle.io.File; which behaves like java.io.File to make a zip look like a directory tree.

        // Arrange for zip extension files to be treated as virtual directories.
        final File zipFile = new File( args[ 0 ], new DefaultArchiveDetector( "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 File toAdd = new File( args[ 1 ], DefaultArchiveDetector.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
        File zipEntry = new File( zipFile, toAdd.getName(), ArchiveDetector.NULL );
        out.println( "about to copy " + toAdd.getCanOrAbsPath() + " to zipentry: " + zipEntry.getCanOrAbsPath() );
        // add a file to the zip.
        if ( !toAdd.copyTo( zipEntry ) )
            {
            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.File[]
        final File[] contents = ( File[] ) zipFile.listFiles();
        out.println( "contents of zip:" );
        for ( File content : contents )
            {
            out.println( content.getCanOrAbsPath() );
            }
        // do the physical i/o to build the zip and close.
        File.umount();
        }
    }