Description
Uses code fragments from Jakarta-Ant, Copyright: Apache Software Foundation.
License
Open Source License
Parameter
Parameter | Description |
---|
out | the out |
stripPath | the strip path |
dir | the dir |
pathSeparator | the path separator |
Exception
Parameter | Description |
---|
IOException | Signals that an I/O exception has occurred. |
Declaration
private static void zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
throws IOException
Method Source Code
//package com.java2s;
/*******************************************************************************
* Copyright (c) 2010 Maciej Kaniewski (mk@firegnom.com).
* /*from w ww . j a v a2s .co m*/
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contributors:
* Maciej Kaniewski (mk@firegnom.com) - initial API and implementation
******************************************************************************/
import java.io.BufferedInputStream;
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 {
/**
* Uses code fragments from Jakarta-Ant, Copyright: Apache Software
* Foundation.
*
* @param out the out
* @param stripPath the strip path
* @param dir the dir
* @param pathSeparator the path separator
* @throws IOException Signals that an I/O exception has occurred.
*/
private static void zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
throws IOException {
String[] entries = dir.list();
if (entries == null || entries.length == 0) {
return;
}
// recurse via entries
for (String entry : entries) {
File file = new File(dir, entry);
if (file.isDirectory()) {
zipDirectory(out, stripPath, file, pathSeparator);
} else {
zipFile(out, stripPath, file, pathSeparator);
}
}
}
/**
* Uses code fragments from Jakarta-Ant, Copyright: Apache Software
* Foundation.
*
* @param out the out
* @param stripPath the strip path
* @param file the file
* @param pathSeparator the path separator
* @throws IOException Signals that an I/O exception has occurred.
*/
private static void zipFile(ZipOutputStream out, String stripPath, File file, char pathSeparator)
throws IOException {
ZipEntry ze = new ZipEntry(processPath(file.getPath(), stripPath, pathSeparator));
ze.setTime(file.lastModified());
out.putNextEntry(ze);
byte[] buffer = new byte[8 * 1024];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file), buffer.length);
try {
int count = 0;
while ((count = in.read(buffer, 0, buffer.length)) >= 0) {
if (count != 0) {
out.write(buffer, 0, count);
}
}
} finally {
in.close();
}
}
/**
* Process path.
*
* @param path the path
* @param stripPath the strip path
* @param pathSeparator the path separator
* @return the string
*/
private static String processPath(String path, String stripPath, char pathSeparator) {
if (!path.startsWith(stripPath)) {
throw new IllegalArgumentException("Invalid entry: " + path + "; expected to start with " + stripPath);
}
return path.substring(stripPath.length()).replace(File.separatorChar, pathSeparator);
}
}
Related
- zipDirectory(String dbDumpPath)
- zipDirectory(String dir2zip, ZipOutputStream zos, String zipPath)
- zipDirectory(String directoryName, int iBaseFolderLength, ZipOutputStream zos, CRC32 crc)
- zipDirectory(String directoryName, String targetName)
- zipDirectory(String dirName, String zipFileName)
- zipDirectoryEntry(ZipOutputStream output, IPath entry, long time, Set directoryEntries)
- zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList avoidingFiles)
- zipSubDirectory(String basePath, File dir, ZipOutputStream zout)