Description
Zip a directory and writes it on parameter ZipOutStream.
License
Open Source License
Parameter
Parameter | Description |
---|
bufferSize | specifies the size with which data is being read. |
dir2zip | a parameter |
filter | a parameter |
zos | a parameter |
Exception
Parameter | Description |
---|
IOException | an exception |
Declaration
public static void zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos,
int bufferSize) throws IOException
Method Source Code
//package com.java2s;
/*/*from w ww. j a v a 2 s. c o m*/
* Copyright (c) Fiorano Software Pte. Ltd. and affiliates. All rights reserved. http://www.fiorano.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
import java.io.*;
import java.util.zip.ZipOutputStream;
public class Main {
private static boolean DEBUG = false;
/**
* Zip a directory and writes it on parameter ZipOutStream.
*
* @param bufferSize specifies the size with which data is being read.
* @param dir2zip
* @param filter
* @param zos
* @throws IOException
*/
public static void zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos,
int bufferSize) throws IOException {
// create a new File object based on the directory we have to zip
File zipDir = new File(dir2zip);
zipDir(zipDir, zipDir, filter, zos, bufferSize);
}
/**
* Zip a directory, where relative path is relativeDir
*
* @param bufferSize specifies the size with which data is being read.
* @param zipDir
* @param baseDir
* @param filter
* @param zos
* @throws IOException
*/
private static void zipDir(File zipDir, File baseDir, FilenameFilter filter, java.util.zip.ZipOutputStream zos,
int bufferSize) throws IOException {
if (zipDir.isFile()) {
//File file = zipDir;
String entryName = entryName(zipDir, baseDir);
// add zip entry
_addZipEntry(zos, entryName);
// add file to zip strea,
_addFileToZipStream(zipDir, zos, bufferSize);
} else {
//get a listing of the directory content
String[] dirList = zipDir.list(filter);
//loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
File file = new File(zipDir, dirList[i]);
if (file.isDirectory()) {
String entryName = entryName(file, baseDir);
// add zip entry
_addZipEntry(zos, entryName);
}
zipDir(file, baseDir, filter, zos, bufferSize);
}
}
}
/**
* Converts file pathname to a form acceptable to ZIP files.
* In particular, file separators are converted to forward slashes.
*
* @param file the file to be converted.
* @param relativeDir the dir which will be referenced when
* caculates the returned file path
* @return a relative path of the given file according to
* the specified relativeDir.
* <p/>
* For exsample: - If the absolute path of a file
* is e:\\a\\b\\c\\file.suf, and the relativeDir is
* e:\\a\\b, then "c/file.suf" will be returned.
* <p/>
* if the String which presents a path name contains
* "\\" as separator, "\\" will be replaced by "/"
* @throws IOException
*/
private static String entryName(File file, File relativeDir) {
if (relativeDir.isFile())
return file.getName();
String fileName = file.getAbsolutePath();
String relativePath = relativeDir.getAbsolutePath();
if (!relativePath.endsWith(File.separator)) {
relativePath += File.separator;
}
fileName = fileName.substring(relativePath.length());
if ((file.isDirectory()) && (!fileName.endsWith(File.separator)))
fileName += File.separator;
return fileName.replace(File.separatorChar, '/');
}
private static void _addZipEntry(ZipOutputStream zos, String entryName) throws IOException {
// create a new zip entry
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(entryName);
if (DEBUG)
System.out.println("entry.name.0");
// place the zip entry in the ZipOutputStream object
zos.putNextEntry(zipEntry);
}
/**
* Add file to parameter zipStream
*
* @param file
* @param zos
* @param bufferSize
* @throws IOException
*/
private static void _addFileToZipStream(File file, ZipOutputStream zos, int bufferSize) throws IOException {
byte[] readBuffer = new byte[bufferSize];
int bytesIn = 0;
if (file.canRead()) {
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try {
// write the content of the file to the ZipOutputStream
while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}
} finally {
// Close the Stream
fis.close();
}
} else {
System.out.println("unable.to.read.file.0");
}
}
}
Related
- zipDir(File zipFile, File dir, boolean includeRoot)
- zipDir(File zipFile, File dirObj)
- zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
- zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
- zipDir(String dir, ZipOutputStream zos)
- zipDir(String dir2zip, String zipFileName)
- zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
- zipDir(String dir2zip, ZipOutputStream zos)
- zipDir(String directory, String zipName)