001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019
020
021package pt.ua.dicoogle.core;
022
023import java.io.BufferedInputStream;
024import java.io.BufferedOutputStream;
025import java.io.FileInputStream;
026import java.io.FileNotFoundException;
027import java.io.FileOutputStream;
028import java.io.IOException;
029import org.slf4j.LoggerFactory;
030import java.util.zip.Adler32;
031import java.util.zip.CheckedInputStream;
032import java.util.zip.ZipEntry;
033import java.util.zip.ZipInputStream;
034
035/**
036 *
037 * @author Luís A. Bastião Silva <bastiao@ua.pt>
038 */
039public class UnZip
040{
041    final int BUFFER = 2048;
042
043    private String filePath = null;
044    private ZipInputStream zis = null;
045    private BufferedOutputStream dest = null;
046    private CheckedInputStream checksum = null;
047
048
049    public UnZip(String filePath)
050    {
051        this.filePath = filePath;
052    }
053
054    public void loadFile()
055    {
056
057        FileInputStream fis = null;
058        try
059        {
060            fis = new FileInputStream(this.filePath);
061        }
062        catch (FileNotFoundException ex) {
063            LoggerFactory.getLogger(UnZip.class).error(ex.getMessage(), ex);
064        }
065        checksum = new CheckedInputStream(fis, new Adler32());
066        
067        
068        zis = new ZipInputStream(new BufferedInputStream(checksum));
069
070      }
071        
072
073
074    public void decompress()
075    {
076
077
078        if (zis==null)
079            return;
080        
081        ZipEntry entry;
082        try {
083            while ((entry = zis.getNextEntry()) != null) {
084                System.out.println("Extracting: " + entry);
085                int count;
086                byte[] data = new byte[BUFFER];
087                // write the files to the disk
088                FileOutputStream fos = new FileOutputStream(entry.getName());
089                dest = new BufferedOutputStream(fos, BUFFER);
090                while ((count = zis.read(data, 0, BUFFER)) != -1) {
091                    dest.write(data, 0, count);
092                }
093                dest.flush();
094                dest.close();
095            }
096        } catch (IOException ex) {
097            LoggerFactory.getLogger(UnZip.class).error(ex.getMessage(), ex);
098        }
099
100
101    }
102
103    public void close()
104    {
105
106        try
107        {
108            zis.close();
109        } catch (IOException ex)
110        {
111            LoggerFactory.getLogger(UnZip.class).error(ex.getMessage(), ex);
112        }
113        System.out.println("Checksum:"+checksum.getChecksum().getValue());
114
115
116    }
117
118}