public static void zip( File[] sourceFiles, File destFile, boolean deleteOriginalFiles ) throws IOException { zip( sourceFiles, destFile, deleteOriginalFiles, false ); }
/** * Uncompresses zipped files * @param zippedFile The file to uncompress * @return list of unzipped files * @throws java.io.IOException thrown if there is a problem finding or writing the files */ public static List<File> unzip(File zippedFile) throws IOException { return unzip(zippedFile, null); }
static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException { File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip, false); } else { byte[] buf = new byte[1024]; int len; FileInputStream in = new FileInputStream(srcFile); try{ zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); while ((len = in.read(buf)) > 0) { zip.write(buf, 0, len); } } finally { in.close(); } } }
/** * Zip the subdirectory and exclude already zipped files * @param path * @param srcFolder * @param zip * @param includeFullPath * @throws IOException */ static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath) throws IOException { File folder = new File(srcFolder); for (String fileName : folder.list()) { if (path.equals("") && !fileName.endsWith(".zip")) { if (includeFullPath){ addFileToZip(folder.toString(), srcFolder + "/" + fileName, zip); } else { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip); } } else if (!fileName.endsWith(".zip")) { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip); } } }
addFolderToZip("", sourceFile.getAbsolutePath(), out, includeFullPathName);
CompressionUtils.zip(xmlFiles.toArray(new File[xmlFiles.size()]), zipFile, false,false); } catch ( IOException e ) { e.printStackTrace();