Here you can find the source of addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
Parameter | Description |
---|---|
path | the path |
srcFolder | the src folder |
zip | the zip |
private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
//package com.java2s; /*/*from w w w .j av a 2 s.c om*/ * FileUtil.java * Copyright (c) 2014, CODEROAD, All Rights Reserved. * * This software is the confidential and proprietary information of CODEROAD * ("Confidential Information"). You shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement you entered into with * CODEROAD. */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** The Constant SLASH. */ private static final String SLASH = "/"; /** * Adds the folder to zip. * * @param path the path * @param srcFolder the src folder * @param zip the zip */ private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) { final File folder = new File(srcFolder); if (folder.list().length == 0) { addFileToZip(path, srcFolder, zip, true); } else { for (String fileName : folder.list()) { if (path.equals("")) { addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false); } else { addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false); } } } } /** * Adds the file to zip. * * @param path the path * @param srcFile the src file * @param zip the zip * @param flag the flag */ private static void addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag) { try { final File folder = new File(srcFile); if (folder.isDirectory()) { addFolderToZip(path, srcFile, zip); } else { final byte[] buf = new byte[1024]; int len; final FileInputStream inputStream = new FileInputStream(srcFile); zip.putNextEntry( new ZipEntry(new StringBuilder(path).append(SLASH).append(folder.getName()).toString())); while (inputStream.read(buf) > 0) { len = inputStream.read(buf); zip.write(buf, 0, len); } inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }