Java examples for File Path IO:Zip File
Adds an entry at the root of the ZIP.
/*// w ww . jav a 2 s . c o m * JBoss, Home of Professional Open Source. * Copyright 2015, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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; public class Main{ public static final String ENTRY_SEPARATOR = "/"; /** * Adds an entry at the root of the ZIP. * * @param fileOrDir file or directory * @param zos target zip * @throws IOException */ public static void addToZip(File fileOrDir, ZipOutputStream zos) throws IOException { addToZip(fileOrDir, null, zos); } /** * Adds an entry to the ZIP at the specified path. * If the path is null, the entry will added at the root of the ZIP. * * @param fileOrDir file or directory * @param path path relative to the root of the ZIP * @param zos target ZIP * @throws IOException */ public static void addToZip(File fileOrDir, String path, ZipOutputStream zos) throws IOException { if (fileOrDir.isDirectory()) { final String dirName = path == null ? fileOrDir.getName() : path + ENTRY_SEPARATOR + fileOrDir.getName(); addDirectoryToZip(fileOrDir, dirName, zos); } else { addFileToZip(fileOrDir, path, zos); } } private static void addDirectoryToZip(File dir, String dirName, ZipOutputStream zos) throws IOException { final ZipEntry dirEntry = new ZipEntry(dirName + ENTRY_SEPARATOR); zos.putNextEntry(dirEntry); zos.closeEntry(); File[] children = dir.listFiles(); if (children != null) { for (File file : children) { if (file.isDirectory()) { addDirectoryToZip(file, dirName + ENTRY_SEPARATOR + file.getName(), zos); } else { addFileToZip(file, dirName, zos); } } } } private static void addFileToZip(File file, String parent, ZipOutputStream zos) throws IOException { final FileInputStream is = new FileInputStream(file); try { final String entryName = parent == null ? file.getName() : parent + ENTRY_SEPARATOR + file.getName(); zos.putNextEntry(new ZipEntry(entryName)); final BufferedInputStream bis = new BufferedInputStream(is); try { IoUtils.copyStream(bis, zos); } finally { IoUtils.safeClose(bis); } zos.closeEntry(); } finally { IoUtils.safeClose(is); } } }