/*
 * [TestTrueVFS.java]
 *
 * Summary: Example use of TrueVFS.
 *
 * Copyright: (c) 2012-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.7+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2012-11-23 initial version
 */
package com.mindprod.example;

import net.java.truevfs.access.TArchiveDetector;
import net.java.truevfs.access.TFile;
import net.java.truevfs.access.TVFS;

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

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

/**
 * Example use of TrueVFS.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2012-11-23 initial version
 * @since 2011-11-23
 */
public final class TestTrueVFS
    {
    /**
     * Example use of TrueVFS
     * <p/>
     * You must first download the truevfs-profile-default-0.10.1-shaded.jar
     * from http://truevfs.java.net/kick-start/no-maven.html
     * put it in your ext directories.
     * You also need slf4j-nop-1.7.2.jar to turn off logging.
     * <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 TrueVFS compatible ZIP file.
     *
     * @param args name of a TrueVFS-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
        {
        // need some code here to turn off logging.
        // 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 TrueVFS 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
        final 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 = 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.
        TVFS.umount();
        }
    }