Description
Zip directory content in the given output stream
License
Open Source License
Parameter
Parameter | Description |
---|
zos | the output stream in which to store the zipped directory |
directoriesAndFiles | a list of directories and files to be zipped |
Exception
Parameter | Description |
---|
IOException | if a zip entry cannot be created. |
Declaration
protected static void zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc) throws IOException
Method Source Code
//package com.java2s;
/*/* ww w . j ava 2 s . 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 {
/**
* Zip directory content in the given output stream
*
* @param zos the output stream in which to store the zipped directory
* @param directoriesAndFiles a list of directories and files to be zipped
* @throws IOException if a zip entry cannot be created.
*/
protected static void zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc) throws IOException {
for (String pathElement : directoriesAndFiles) {
File fileElement = new File(pathElement);
//normalize path (also remove consecutive file separator)
pathElement = fileElement.getPath();
int length = pathElement.lastIndexOf(File.separator) + 1;
if (fileElement.isFile()) {
// add zip files at the root of the global jar file !
zipFile(pathElement, length, zos, crc);
} else if (fileElement.isDirectory()) {
// get only last directory
zipDirectory(pathElement, length, zos, crc);
}
}
}
/**
* 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.
}
}
/**
* 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.");
}
}
}
Related
- zipFile(ZipOutputStream zos, BufferedOutputStream out, File destPath, String originalPath)
- zipFile(ZipOutputStream zos, File file, String requestName, byte[] buf)
- zipFile(ZipOutputStream zout, File file, int rootLength)
- zipInternal(String path, File file, ZipOutputStream os)
- zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)
- zipOneFile(File file, String base, ZipOutputStream zout)
- zipOneFile(File file, ZipOutputStream out, String relativePath)
- zipOneFile(String prefix, File file, ZipOutputStream zos)
- zipRecurse(File file, int prefixLength, ZipOutputStream zos)