Mega Code Archive
Util for extracting jar, war and zip archives
//Copyright 2007-2008 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed 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 com.dyuproject.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* Util for extracting *.jar, *.war and *.zip archives.
*
* @author David Yu
* @created Feb 25, 2008
*/
public final class ArchiveUtil
{
/**
* The suffixes of the files to support.
*/
public static final String[] SUPPORTED_FILES = new String[]{
".zip",
".jar",
".war"
};
private static File __tempDir;
static
{
__tempDir = new File(System.getProperty("java.io.tmpdir"));
}
public static void setTempDir(File tempDir)
{
if(tempDir.isDirectory() && tempDir.exists())
__tempDir = tempDir;
}
/**
* Gets the temp dir (the sys property "java.io.tmpdir" if not
* overridden via {@link #setTempDir(File)}).
*/
public static File getTempDir()
{
return __tempDir;
}
/**
* Returns true if the given {@code resource} is either a zip, jar or war file.
*/
public static boolean isSupported(String resource)
{
int idx = resource.lastIndexOf('.');
if(resource.length()==idx+4)
{
for(int i=0; i getFilesByExtension(File dir, String[] extensions)
{
if(!dir.isDirectory() || extensions==null)
return Collections.emptyList();
List files = new ArrayList();
addFilesByExtension(files, dir, extensions);
return files;
}
static void addFilesByExtension(List list, File dir, String[] extensions)
{
File[] files = dir.listFiles();
for(int i=0; i