Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * PODD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.resources.services.util; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** * @author Yuan-Fang Li * @version $Id$ */ public class ZipUtility { public File zipDirectory(File zipFile, File directory) throws IOException { final String dirName = directory.getName(); ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(zipFile)); try { doZipDirectory(outStream, directory, dirName); } finally { outStream.close(); } return zipFile; } private void doZipDirectory(ZipOutputStream zos, File directory, String path) throws IOException { for (File file : directory.listFiles()) { if (file.isDirectory()) { doZipDirectory(zos, file, path + File.separator + file.getName()); } else { ZipEntry entry = new ZipEntry(path + File.separator + file.getName()); zos.putNextEntry(entry); InputStream is = new FileInputStream(file); try { IOUtils.copy(is, zos); } finally { IOUtils.closeQuietly(is); } } } } public ZipOutputStream addZipEntry(ZipOutputStream zos, String name, InputStream is) throws IOException { ZipEntry entry = new ZipEntry(name); zos.putNextEntry(entry); try { IOUtils.copy(is, zos); } finally { IOUtils.closeQuietly(is); } return zos; } public File addZipEntry(File zipFile, String name, InputStream is) throws IOException { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); addZipEntry(zos, name, is); zos.close(); return zipFile; } public void unzipFile(File unzipDir, File zipFile) throws IOException { ZipFile zippedFile = new ZipFile(zipFile); try { final Enumeration<? extends ZipEntry> entries = zippedFile.entries(); while (entries.hasMoreElements()) { final ZipEntry zipEntry = entries.nextElement(); if (zipEntry.isDirectory()) { File newDir = new File(zipEntry.getName()); //System.out.println("newDir = " + newDir.getPath()); newDir.mkdir(); } else { final InputStream is = zippedFile.getInputStream(zipEntry); File curFile = new File(unzipDir, zipEntry.getName()); if (!curFile.getParentFile().exists()) { curFile.getParentFile().mkdirs(); } final FileOutputStream os = new FileOutputStream(curFile); copyStream(is, os); } } } finally { zippedFile.close(); } } private void copyStream(InputStream is, FileOutputStream fos) throws IOException { try { IOUtils.copy(is, fos); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(fos); } } }