/*
 * IzPack - Copyright 2001-2012 Julien Ponge, All Rights Reserved.
 *
 * http://izpack.org/
 * http://izpack.codehaus.org/
 *
 * Copyright 2012 Tim Anderson
 *
 * 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.
 */

package com.izforge.izpack.installer.unpacker;

import com.izforge.izpack.api.data.PackCompression;
import com.izforge.izpack.api.data.PackFile;
import com.izforge.izpack.api.exception.InstallerException;
import com.izforge.izpack.util.os.FileQueue;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream;
import org.apache.commons.compress.compressors.deflate.DeflateParameters;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;


/**
 * Unpacker for compressed files.
 */
public class CompressedFileUnpacker extends FileUnpacker
{
    private final PackCompression compressionFormat;

    /**
     * Constructs a <tt>CompressedFileUnpacker</tt>.
     *
     * @param cancellable determines if unpacking should be cancelled
     * @param queue       the file queue. May be <tt>null</tt>
     */
    public CompressedFileUnpacker(Cancellable cancellable, FileQueue queue, PackCompression compressionFormat)
    {
        super(cancellable, queue);
        this.compressionFormat = compressionFormat;
    }

    /**
     * Unpacks a pack file.
     *
     * @param file            the pack file meta-data
     * @param packInputStream the pack input stream
     * @param target          the target
     * @throws IOException        for any I/O error
     * @throws InstallerException for any installer exception
     */
    @Override
    public void unpack(PackFile file, InputStream packInputStream, File target)
            throws IOException, InstallerException
    {
        File tmpfile = File.createTempFile("izpack-uncompress", null, FileUtils.getTempDirectory());
        OutputStream fo = null;
        InputStream finalStream = null;

        try
        {
            fo = IOUtils.buffer(FileUtils.openOutputStream(tmpfile));
            IOUtils.copyLarge(packInputStream, fo, 0, file.size());
            fo.flush();
            fo.close();

            InputStream in = IOUtils.buffer(FileUtils.openInputStream(tmpfile));

            if (compressionFormat == PackCompression.DEFLATE)
            {
                DeflateParameters deflateParameters = new DeflateParameters();
                deflateParameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
                finalStream = new DeflateCompressorInputStream(in, deflateParameters);
            }
            else
            {
                finalStream = new CompressorStreamFactory().createCompressorInputStream(compressionFormat.toName(), in);
            }

            copy(file, finalStream, target);
        }
        catch (CompressorException e)
        {
            throw new IOException(e);
        }
        finally
        {
            IOUtils.closeQuietly(fo);
            IOUtils.closeQuietly(finalStream);
            FileUtils.deleteQuietly(tmpfile);
        }
    }
}

@@ -3628,16 +3628,115 @@
 ception(
+%22An exception occurred whilst unpacking: %22 + file.getRelativeSourcePath() + %22: %22 + e.getMessage(), 
 e);%0A    

Improve error message when unpacking fails