/*
 * [TestTrueZIP6.java]
 *
 * Summary: Example use of TrueZIP version 6.
 *
 * Copyright: (c) 2009-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 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 version 6.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-05-14 initial version
 * @since 2009-05-14
 */
public final class TestTrueZIP6
    {
    // --------------------------- main() method ---------------------------

    /**
     * Example use of TrueZIP 6
     * <p/>
     * You must first download the truezip-6.8.1.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.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
        {
        // "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();
        }
    }