/* 
 * Diplomová práce ÚSI VUT 
 * Forenzní analýza webového prohlížeče
 *
 * Autor: Michal Bača
 * Datum: 20.5.2015
 */

package yafft.Utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author michal
 */
public class Zipper {

    List<Path> files;

    Path outpuDir;
    File baseDir;
    String fileName;
    String extension = ".zip";

    private ZipOutputStream zipOut;
    private FileOutputStream fileOut;

    public Zipper(List<Path> files, Path outpuDir, File baseDir, String fileName) {
        this.files = files;
        this.outpuDir = outpuDir;
        this.fileName = fileName;
        this.baseDir = baseDir;

    }

    public List<Path> getFiles() {
        return files;
    }

    public void setFiles(List<Path> files) {
        this.files = files;
    }

    public Path getOutpuDir() {
        return outpuDir;
    }

    public void setOutpuDir(Path outpuDir) {
        this.outpuDir = outpuDir;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    private void addFile(File f) throws FileNotFoundException, IOException {
        String file = this.baseDir.toURI().relativize(f.toURI()).getPath();
                
        System.out.println("Adding " + file + " to ZIP");
        ZipEntry ze = new ZipEntry(file);

        this.zipOut.putNextEntry(ze);

        byte[] data = Files.readAllBytes(f.toPath());

        this.zipOut.write(data);
        this.zipOut.closeEntry();

    }

    public void createZip() throws FileNotFoundException, IOException {

        this.fileOut = new FileOutputStream(outpuDir + java.io.File.separator + this.fileName + this.extension);
        this.zipOut = new ZipOutputStream(this.fileOut);

        for (Path p : this.files) {

            File f = p.toFile();
            if (f.isFile()) {

                this.addFile(f);
            }

        }
        this.zipOut.close();
        this.fileOut.close();
        
        System.out.println("ZIP " + this.outpuDir + "/" + this.fileName + ".zip done");

    }
    
    public String getOutputFileName() {
        return this.outpuDir + "/" + this.fileName + ".zip";
    }

}
