client/ftpd/src/se/lu/thep/coreftpd/common/ExtractZipFile.java

Code
Comments
Other
Rev Date Author Line
741 10 Oct 06 olle 1 /*
1652 22 May 07 gregory 2  $Id$
741 10 Oct 06 olle 3
1916 31 Aug 07 jari 4  Copyright (C) 2006 Olle Mansson
1916 31 Aug 07 jari 5  Copyright (C) 2007 Gregory Vincic
741 10 Oct 06 olle 6
1652 22 May 07 gregory 7  This file is part of Proteios.
1652 22 May 07 gregory 8  Available at http://www.proteios.org/
741 10 Oct 06 olle 9
1652 22 May 07 gregory 10  Proteios is free software; you can redistribute it and/or modify it
1652 22 May 07 gregory 11  under the terms of the GNU General Public License as published by
1652 22 May 07 gregory 12  the Free Software Foundation; either version 2 of the License, or
1652 22 May 07 gregory 13  (at your option) any later version.
741 10 Oct 06 olle 14
1652 22 May 07 gregory 15  Proteios is distributed in the hope that it will be useful, but
1652 22 May 07 gregory 16  WITHOUT ANY WARRANTY; without even the implied warranty of
1652 22 May 07 gregory 17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1652 22 May 07 gregory 18  General Public License for more details.
741 10 Oct 06 olle 19
1652 22 May 07 gregory 20  You should have received a copy of the GNU General Public License
1652 22 May 07 gregory 21  along with this program; if not, write to the Free Software
1652 22 May 07 gregory 22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1652 22 May 07 gregory 23  02111-1307, USA.
1652 22 May 07 gregory 24  */
741 10 Oct 06 olle 25
741 10 Oct 06 olle 26 //  Xerver Free Web Server
741 10 Oct 06 olle 27 //  Copyright (C) 2002-2005 Omid Rouhani
741 10 Oct 06 olle 28 //
741 10 Oct 06 olle 29 //
741 10 Oct 06 olle 30 //  This program is free software; you can redistribute it and/or
741 10 Oct 06 olle 31 //  modify it under the terms of the GNU General Public License
741 10 Oct 06 olle 32 //  as published by the Free Software Foundation; either version 2
741 10 Oct 06 olle 33 //  of the License, or (at your option) any later version.
741 10 Oct 06 olle 34 //
741 10 Oct 06 olle 35 //  This program is distributed in the hope that it will be useful,
741 10 Oct 06 olle 36 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
741 10 Oct 06 olle 37 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
741 10 Oct 06 olle 38 //  GNU General Public License for more details.
741 10 Oct 06 olle 39 //
741 10 Oct 06 olle 40 //  You should have received a copy of the GNU General Public License
741 10 Oct 06 olle 41 //  along with this program; if not, write to the Free Software
741 10 Oct 06 olle 42 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
741 10 Oct 06 olle 43 //
741 10 Oct 06 olle 44 //
741 10 Oct 06 olle 45 //  #############################################################
741 10 Oct 06 olle 46 //  ##  YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT:          ##
741 10 Oct 06 olle 47 //  ##  HTTP://WWW.JAVASCRIPT.NU/XERVER/                       ##
741 10 Oct 06 olle 48 //  ##                                                         ##
741 10 Oct 06 olle 49 //  ##  IF YOUR SOFTWARE IS NOT RELEASED UNDER THE             ##
741 10 Oct 06 olle 50 //  ##  GNU GENERAL PUBLIC LICENSE (GPL),                      ##
741 10 Oct 06 olle 51 //  ##  PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!!   ##
741 10 Oct 06 olle 52 //  ##                                                         ##
741 10 Oct 06 olle 53 //  ##  FOR FULL LICENSE, PLEASE READ "XERVER LICENSE".        ##
741 10 Oct 06 olle 54 //  #############################################################
741 10 Oct 06 olle 55
1652 22 May 07 gregory 56 package se.lu.thep.coreftpd.common;
741 10 Oct 06 olle 57
791 23 Oct 06 olle 58 import se.lu.thep.coreftpd.webserver.ReadInputStream;
741 10 Oct 06 olle 59 import java.io.BufferedInputStream;
741 10 Oct 06 olle 60 import java.io.BufferedOutputStream;
741 10 Oct 06 olle 61 import java.io.File;
741 10 Oct 06 olle 62 import java.io.FileOutputStream;
741 10 Oct 06 olle 63 import java.util.Enumeration;
741 10 Oct 06 olle 64 import java.util.zip.ZipEntry;
741 10 Oct 06 olle 65 import java.util.zip.ZipFile;
741 10 Oct 06 olle 66
741 10 Oct 06 olle 67 /**
1652 22 May 07 gregory 68  * 
1652 22 May 07 gregory 69  * <B>How to use:</B> <BR>
1652 22 May 07 gregory 70  * <CODE>new ExtractZipFile(String zipFileName, boolean b_overwriteFiles);</CODE>
741 10 Oct 06 olle 71  * <BR>
1652 22 May 07 gregory 72  * <CODE>new ExtractZipFile(File zipFile, boolean b_overwriteFiles);</CODE>
741 10 Oct 06 olle 73  * <BR>
741 10 Oct 06 olle 74  * <BR>
1652 22 May 07 gregory 75  * 
1652 22 May 07 gregory 76  * <B>More info:</B> <BR>
1652 22 May 07 gregory 77  * <CODE>ExtractZipFile</CODE> opens a zip-file and extracts the files in the
1652 22 May 07 gregory 78  * zip-file. <BR>
1652 22 May 07 gregory 79  * This class does also support zip-files with directories (directories will be
1652 22 May 07 gregory 80  * created and files moved into the directories). <BR>
1652 22 May 07 gregory 81  * Set <CODE>b_overwriteFiles = true</CODE> and files will be overwritten if
1652 22 May 07 gregory 82  * they already exists. <BR>
1652 22 May 07 gregory 83  * Set <CODE>b_overwriteFiles = false</CODE> and files will NOT be overwritten
1652 22 May 07 gregory 84  * if they already exists.
1652 22 May 07 gregory 85  * 
741 10 Oct 06 olle 86  * @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a>
741 10 Oct 06 olle 87  * @version 1.0
741 10 Oct 06 olle 88  */
741 10 Oct 06 olle 89
1652 22 May 07 gregory 90 public class ExtractZipFile {
1652 22 May 07 gregory 91   protected final boolean b_showErrors = false;
741 10 Oct 06 olle 92
1652 22 May 07 gregory 93   protected boolean b_overwriteFiles = false; // this will be set in the
1652 22 May 07 gregory 94                         // constructor as well...
1652 22 May 07 gregory 95
1652 22 May 07 gregory 96   public ExtractZipFile(String s, boolean argOverwriteFiles) {
741 10 Oct 06 olle 97     this(new File(s), argOverwriteFiles);
741 10 Oct 06 olle 98   }
741 10 Oct 06 olle 99
1652 22 May 07 gregory 100   public ExtractZipFile(File zipFile, boolean argOverwriteFiles) {
741 10 Oct 06 olle 101     this(zipFile, argOverwriteFiles, null);
741 10 Oct 06 olle 102   }
741 10 Oct 06 olle 103
1652 22 May 07 gregory 104   public ExtractZipFile(String s, boolean argOverwriteFiles,
1652 22 May 07 gregory 105       String s_defaultPath) {
741 10 Oct 06 olle 106     this(new File(s), argOverwriteFiles, s_defaultPath);
741 10 Oct 06 olle 107   }
741 10 Oct 06 olle 108
1652 22 May 07 gregory 109   public ExtractZipFile(File zipFile, boolean argOverwriteFiles,
1652 22 May 07 gregory 110       String s_defaultPath) {
741 10 Oct 06 olle 111     try {
1652 22 May 07 gregory 112       if (s_defaultPath == null)
1652 22 May 07 gregory 113         s_defaultPath = "";
1652 22 May 07 gregory 114       else if ((new File(s_defaultPath)).exists())
1652 22 May 07 gregory 115         s_defaultPath = (new File(s_defaultPath)).getPath()
1652 22 May 07 gregory 116             + File.separator;
1652 22 May 07 gregory 117       else
1652 22 May 07 gregory 118         s_defaultPath = "";
741 10 Oct 06 olle 119
1652 22 May 07 gregory 120       b_overwriteFiles = argOverwriteFiles;
1652 22 May 07 gregory 121       ZipFile ZF_theZipFile = new ZipFile(zipFile);
1652 22 May 07 gregory 122       // Enumeration e_allFiles=ZF_theZipFile.entries();
741 10 Oct 06 olle 123
1652 22 May 07 gregory 124       // System.out.println("Folders:");
1652 22 May 07 gregory 125       // First, create all folders
1652 22 May 07 gregory 126       for (Enumeration e = ZF_theZipFile.entries(); e.hasMoreElements();) {
1652 22 May 07 gregory 127         ZipEntry myZipEntry = (ZipEntry) e.nextElement();
1652 22 May 07 gregory 128         if (myZipEntry.isDirectory()) {
1652 22 May 07 gregory 129           File newDir = new File(s_defaultPath + myZipEntry.getName());
1652 22 May 07 gregory 130           newDir.mkdirs();
1652 22 May 07 gregory 131         }
741 10 Oct 06 olle 132       }
741 10 Oct 06 olle 133
1652 22 May 07 gregory 134       // System.out.println("Files:");
1652 22 May 07 gregory 135       // Then extract all the files
1652 22 May 07 gregory 136       for (Enumeration e = ZF_theZipFile.entries(); e.hasMoreElements();) {
1652 22 May 07 gregory 137         ZipEntry myZipEntry = (ZipEntry) e.nextElement();
1652 22 May 07 gregory 138         if (!myZipEntry.isDirectory()
1652 22 May 07 gregory 139             && (overWriteFile(myZipEntry) || !(new File(myZipEntry
1652 22 May 07 gregory 140                 .getName())).exists())) // if [this is not a
1652 22 May 07 gregory 141                             // directory] and [[we
1652 22 May 07 gregory 142                             // shall overwrite] or
1652 22 May 07 gregory 143                             // [this file doesn't
1652 22 May 07 gregory 144                             // exists]]...
1652 22 May 07 gregory 145         {
1652 22 May 07 gregory 146           BufferedInputStream bis = new BufferedInputStream(
1652 22 May 07 gregory 147               ZF_theZipFile.getInputStream(myZipEntry));
1652 22 May 07 gregory 148           BufferedOutputStream bos;
1652 22 May 07 gregory 149           bos = new BufferedOutputStream(new FileOutputStream(
1652 22 May 07 gregory 150               s_defaultPath + myZipEntry.getName()));
741 10 Oct 06 olle 151
1652 22 May 07 gregory 152           (new ReadInputStream(bis, bos)).start();
1652 22 May 07 gregory 153         }
741 10 Oct 06 olle 154       }
1652 22 May 07 gregory 155     } catch (Exception e) {
1652 22 May 07 gregory 156       if (b_showErrors)
1652 22 May 07 gregory 157         System.out.println("An error has occured:\nProbably, \""
1652 22 May 07 gregory 158             + zipFile.getPath()
1652 22 May 07 gregory 159             + "\" was not found.\nThe error output is: " + e);
741 10 Oct 06 olle 160     }
741 10 Oct 06 olle 161   }
741 10 Oct 06 olle 162
1652 22 May 07 gregory 163   boolean overWriteFile(ZipEntry myZipEntry) {
741 10 Oct 06 olle 164     return b_overwriteFiles;
741 10 Oct 06 olle 165   }
741 10 Oct 06 olle 166 }