diff options
author | tkellner <tkellner@3a0b52a2-8410-0410-bc02-ff6273a87459> | 2012-10-01 08:30:47 +0000 |
---|---|---|
committer | tkellner <tkellner@3a0b52a2-8410-0410-bc02-ff6273a87459> | 2012-10-01 08:30:47 +0000 |
commit | 139feeaa682e550538214cf1e83b755de15a9f6e (patch) | |
tree | bf18bc873a3a5ecdc08175a0cb531716fa027333 /trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui | |
parent | af4a41ec16c11c88d1afd41f1f9890dd9d2645ed (diff) | |
download | pdf-over-139feeaa682e550538214cf1e83b755de15a9f6e.tar.gz pdf-over-139feeaa682e550538214cf1e83b755de15a9f6e.tar.bz2 pdf-over-139feeaa682e550538214cf1e83b755de15a9f6e.zip |
Added Unzipper class for PDFAS configuration extraction
git-svn-id: https://svn.iaik.tugraz.at/svn/egiz/prj/current/12PDF-OVER-4.0@12546 3a0b52a2-8410-0410-bc02-ff6273a87459
Diffstat (limited to 'trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui')
-rw-r--r-- | trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Unzipper.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Unzipper.java b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Unzipper.java new file mode 100644 index 00000000..af16c8a1 --- /dev/null +++ b/trunk/pdf-over-gui/src/main/java/at/asit/pdfover/gui/Unzipper.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012 by A-SIT, Secure Information Technology Center Austria + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + */ +package at.asit.pdfover.gui; + +// Imports +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class Unzipper { + /** + * SLF4J Logger instance + **/ + private static final Logger log = LoggerFactory.getLogger(Unzipper.class); + + /** + * Extracts Zip File input stream to target path + * @param is + * @param targetPath + * @throws IOException + */ + public static void unzip(InputStream is, String targetPath) throws IOException { + ZipInputStream zis = new ZipInputStream(is); + ZipEntry entry; + // while there are entries I process them + while ((entry = zis.getNextEntry()) != null) { + log.debug("entry: " + entry.getName() + ", " //$NON-NLS-1$//$NON-NLS-2$ + + entry.getSize()); + // consume all the data from this entry + + if (entry.isDirectory()) { + log.debug("Extracting directory: " + entry.getName()); //$NON-NLS-1$ + // This is not robust, just for demonstration purposes. + File nDir =new File(targetPath + "/" + entry.getName()); //$NON-NLS-1$ + if(!nDir.exists()) { + if(!nDir.mkdir()) { + throw new IOException("Failed to create dir: " + entry.getName()); //$NON-NLS-1$ + } + } + continue; + } + byte[] buffer = new byte[1024]; + int len; + BufferedOutputStream out = new BufferedOutputStream( + new FileOutputStream(targetPath + "/" + entry.getName())); //$NON-NLS-1$ + while ((len = zis.read(buffer)) >= 0) + out.write(buffer, 0, len); + + out.close(); + } + } +} |