Here you can find the source of addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
Parameter | Description |
---|---|
path | a parameter |
srcFolder | a parameter |
zip | a parameter |
includeFullPath | a parameter |
Parameter | Description |
---|
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath) throws IOException
//package com.java2s; /*//from w w w.j a v a 2 s. c o m * Copyright (c) 2002-2006 The European Bioinformatics Institute, and others. * All rights reserved. Please see the file LICENSE * in the root directory of this distribution. */ import java.io.*; import java.util.zip.*; public class Main { /** * Zip the subdirectory and exclude already zipped files * @param path * @param srcFolder * @param zip * @param includeFullPath * @throws java.io.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); } } } 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(); } } } }