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
020package pt.ua.dicoogle.core;
021
022import java.io.BufferedInputStream;
023import java.io.BufferedOutputStream;
024import java.io.Closeable;
025import java.io.File;
026import java.io.FileInputStream;
027import java.io.FileNotFoundException;
028import java.io.FileOutputStream;
029import java.io.IOException;
030import org.slf4j.LoggerFactory;
031import java.util.zip.ZipEntry;
032import java.util.zip.ZipOutputStream;
033
034/**
035 *
036 * @author Luís A. Bastião Silva <bastiao@ua.pt>
037 */
038public class Zip implements Closeable
039{
040
041    static final int BUFFER = 2048;
042
043    private BufferedInputStream origin = null;
044    private ZipOutputStream out = null;
045
046    private String zipName = null;
047
048    private boolean createFile = false;
049
050    public Zip(String  zipName)
051    {
052        this.zipName = zipName;
053    }
054
055    public void createZip()
056    {
057
058        // just do it one-time.
059        if (createFile)
060            return;
061
062        ServerSettings ss = ServerSettings.getInstance() ;
063
064        try 
065        {
066            System.out.println("DIR: "+getZipName());
067            FileOutputStream dest = new FileOutputStream(getZipName());
068            
069             out = new ZipOutputStream(new 
070           BufferedOutputStream(dest));
071
072
073            //out.setMethod(ZipOutputStream.DEFLATED);
074            byte data[] = new byte[BUFFER];
075
076
077            createFile = true;
078            
079        
080        } 
081        catch (Exception e)
082        {
083            e.printStackTrace();
084        }
085
086    }
087
088
089    public void addFile(byte [] arr)
090    {
091
092        
093    }
094
095
096
097    public void addFile(String fullPath)
098    {
099
100        File file = new File(fullPath);
101        byte data[] = new byte[BUFFER];
102        FileInputStream fi = null;
103        try
104        {
105            fi = new FileInputStream(file);
106        } catch (FileNotFoundException ex) {
107            LoggerFactory.getLogger(Zip.class).error(ex.getMessage(), ex);
108        }
109        origin = new BufferedInputStream(fi, BUFFER);
110        ZipEntry entry = new ZipEntry(fullPath);
111        if (out==null)
112            System.err.println("OUT NULL");
113
114
115        if (entry==null)
116            System.err.println("entry NULL");
117
118        try
119        {
120            out.putNextEntry(entry);
121        } catch (IOException ex) {
122            LoggerFactory.getLogger(Zip.class).error(ex.getMessage(), ex);
123        }
124        int count;
125        try {
126            while ((count = origin.read(data, 0, BUFFER)) != -1) {
127                out.write(data, 0, count);
128            }
129        } catch (IOException ex) {
130            LoggerFactory.getLogger(Zip.class).error(ex.getMessage(), ex);
131        }
132        try {
133            origin.close();
134        } catch (IOException ex) {
135            LoggerFactory.getLogger(Zip.class).error(ex.getMessage(), ex);
136        }
137
138    }
139
140    public void close()
141    {
142        try {
143            out.close();
144        } catch (IOException ex) {
145            LoggerFactory.getLogger(Zip.class).error(ex.getMessage(), ex);
146        }
147
148    }
149
150    /**
151     * @return the createFile
152     */
153    public boolean isCreateFile() {
154        return createFile;
155    }
156
157    /**
158     * @param createFile the createFile to set
159     */
160    public void setCreateFile(boolean createFile) {
161        this.createFile = createFile;
162    }
163
164    /**
165     * @return the zipName
166     */
167    public String getZipName() {
168        return zipName;
169    }
170
171    /**
172     * @param zipName the zipName to set
173     */
174    public void setZipName(String zipName) {
175        this.zipName = zipName;
176    }
177
178    
179    
180    public static void main(String [] args)
181    {
182    
183    
184        long timeEncryptStart = System.currentTimeMillis();
185        Zip zip = new Zip("/Volumes/Extend/dataset/IM-0001-0001.zip");
186        zip.createZip();
187        zip.addFile("/Volumes/Extend/dataset/XABraga/IM-0001-0001.dcm");
188        
189        zip.close();
190                  
191            
192            long timeEncryptEnd = System.currentTimeMillis() - timeEncryptStart;
193            System.out.println("Encrypted in " + timeEncryptEnd + " (ms)");
194            
195            timeEncryptStart = System.currentTimeMillis();
196            
197            UnZip unzip = new UnZip("/Volumes/Extend/dataset/IM-0001-0001.zip");
198            unzip.loadFile();
199            unzip.decompress();
200            
201            timeEncryptEnd = System.currentTimeMillis() - timeEncryptStart;
202            System.out.println("Encrypted in " + timeEncryptEnd + " (ms)");
203            
204            
205    }
206    
207    
208}