blob: 98b6036cd429fe69b864f50f4aee55d78317a87a [file] [log] [blame]
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.servicemix.kernel.filemonitor;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class JarUtil {
/**
* Zip up a directory
*
* @param directory
* @param zipName
* @throws IOException
*/
public static void zipDir(String directory, String zipName) throws IOException {
// create a ZipOutputStream to zip the data to
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
String path = "";
zipDir(directory, zos, path, Collections.<String>emptySet());
// close the stream
zos.close();
}
/**
* Jar up a directory
*
* @param directory
* @param zipName
* @throws IOException
*/
public static void jarDir(String directory, String zipName) throws IOException {
// create a ZipOutputStream to zip the data to
JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
String path = "";
File manFile = new File(directory, JarFile.MANIFEST_NAME);
if (manFile.exists()) {
byte[] readBuffer = new byte[8192];
FileInputStream fis = new FileInputStream(manFile);
try {
ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
zos.putNextEntry(anEntry);
int bytesIn = fis.read(readBuffer);
while (bytesIn != -1) {
zos.write(readBuffer, 0, bytesIn);
bytesIn = fis.read(readBuffer);
}
} finally {
fis.close();
}
zos.closeEntry();
}
zipDir(directory, zos, path, Collections.singleton(JarFile.MANIFEST_NAME));
// close the stream
zos.close();
}
/**
* Zip up a directory path
* @param directory
* @param zos
* @param path
* @throws IOException
*/
public static void zipDir(String directory, ZipOutputStream zos, String path, Set<String> exclusions) throws IOException {
File zipDir = new File(directory);
// get a listing of the directory content
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[8192];
int bytesIn = 0;
// loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
String filePath = f.getPath();
zipDir(filePath, zos, path + f.getName() + "/", exclusions);
continue;
}
String entry = path + f.getName();
if (!exclusions.contains(entry)) {
FileInputStream fis = new FileInputStream(f);
try {
ZipEntry anEntry = new ZipEntry(entry);
zos.putNextEntry(anEntry);
bytesIn = fis.read(readBuffer);
while (bytesIn != -1) {
zos.write(readBuffer, 0, bytesIn);
bytesIn = fis.read(readBuffer);
}
} finally {
fis.close();
}
}
}
}
}