package org.apache.maven.deploy.util;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Iterator;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
 * @version $Id: Packager.java,v 1.3 2004/03/02 14:59:31 evenisse Exp $
 */
public class Packager
{
    /**
     *
     * @param inputFiles
     * @param outputFiles
     * @param archive
     * @throws IOException
     */
    public static void createZip( List srcFiles, List destFiles, String archive )
        throws IOException
    {
        createZip( srcFiles, destFiles, new File( archive ) );
    }

    /**
        *
        * @param inputFiles
        * @param outputFiles
        * @param archive
        * @throws IOException
        */
    public static void createZip( List srcFiles, List destFiles, File archive )
        throws IOException
    {
        byte[] buf = new byte[1024];

        ZipOutputStream out = null;

        try
        {
            out = new ZipOutputStream( new FileOutputStream( archive ) );
            out.setLevel( Deflater.BEST_COMPRESSION );
            out.setMethod( ZipOutputStream.DEFLATED );

            Iterator iter1 = srcFiles.iterator(  );
            Iterator iter2 = destFiles.iterator(  );

            while ( iter1.hasNext(  ) )
            {
                String inputFilename = ( String ) iter1.next(  );
                String outputFilename = ( String ) iter2.next(  );

                FileInputStream in = new FileInputStream( inputFilename );

                // Add ZIP entry to output stream.
                out.putNextEntry( new ZipEntry( outputFilename ) );

                // Transfer bytes from the file to the ZIP file
                int len;

                while ( ( len = in.read( buf ) ) > 0 )
                {
                    out.write( buf, 0, len );
                }

                // Complete the entry
                out.closeEntry(  );
                in.close(  );
            }
        }

        // Complete the ZIP file
        finally
        {
            if ( out != null )
            {
                try
                {
                    out.close(  );
                }
                catch ( Exception e )
                {
                }
            }
        }
    }
}
