Description
Add the given directory into the zipStream.
License
Open Source License
Parameter
Parameter | Description |
---|
directoryName | the directory to be added in the zip. |
iBaseFolderLength | the index in the directoryName from which starts the actual zip entry name. |
zos | the stream to write into. |
crc | the CRC32 of all zipentries. Can be null if no crc is needed. |
Exception
Parameter | Description |
---|
IOException | if the zip file cannot be written. |
Declaration
protected static void zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
throws IOException
Method Source Code
//package com.java2s;
/*// w w w . j a va 2s. co m
* ProActive Parallel Suite(TM):
* The Open Source library for parallel and distributed
* Workflows & Scheduling, Orchestration, Cloud Automation
* and Big Data Analysis on Enterprise Grids & Clouds.
*
* Copyright (c) 2007 - 2017 ActiveEon
* Contact: contact@activeeon.com
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation: version 3 of
* the License.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* If needed, contact us to obtain a release under GPL Version 2 or 3
* or a different license than the AGPL.
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
public class Main {
/**
* Add the given directory into the zipStream.
*
* @param directoryName the directory to be added in the zip.
* @param iBaseFolderLength the index in the directoryName from which starts the actual zip entry name.
* @param zos the stream to write into.
* @param crc the CRC32 of all zipentries. Can be null if no crc is needed.
* @throws IOException if the zip file cannot be written.
*/
protected static void zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
throws IOException {
File dirobject = new File(directoryName);
if (dirobject.exists()) {
if (dirobject.isDirectory()) {
File[] fileList = dirobject.listFiles();
// Loop through the files
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
zipDirectory(fileList[i].getPath(), iBaseFolderLength, zos, crc);
} else if (fileList[i].isFile()) {
zipFile(fileList[i].getPath(), iBaseFolderLength, zos, crc);
}
}
} else {
throw new IOException(directoryName + " is not a directory.");
}
} else {
throw new IOException("Directory " + directoryName + " does not exist.");
}
}
/**
* Add a file into a zip.
* @param filePath the file to be added in the zip.
* @param iBaseFolderLength the index in the directoryName from which starts the actual zip entry name.
* @param jos the stream to write into.
* @param crc the CRC32 of all zipentries. Can be null if no crc is needed.
* @throws IOException if the zip file cannot be written.
*/
protected static void zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)
throws IOException {
try {
FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis);
String fileNameEntry = filePath.substring(iBaseFolderLength).replace(File.separatorChar, '/');
ZipEntry fileEntry = new ZipEntry(fileNameEntry);
jos.putNextEntry(fileEntry);
byte[] data = new byte[1024];
int byteCount;
while ((byteCount = bis.read(data, 0, 1024)) > -1) {
if (crc != null) {
crc.update(data);
}
jos.write(data, 0, byteCount);
}
jos.closeEntry();
fis.close();
} catch (ZipException e) {
// TODO Other exceptions ?
// Duplicate entry : ignore it.
}
}
}
Related
- zipDirectory(File srcDir, File destFile)
- zipDirectory(File zipDir, ZipOutputStream zos)
- zipDirectory(final String sourceFolder, final String targetFolder, final String zipExtension)
- zipDirectory(String dbDumpPath)
- zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
- zipDirectory(String directoryName, String targetName)
- zipDirectory(String dirName, String zipFileName)
- zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
- zipDirectoryEntry(ZipOutputStream output, IPath entry, long time, Set directoryEntries)