00001
00024 package org.objectweb.jonas_lib.genbase.utils;
00025
00026 import java.io.File;
00027 import java.io.FileInputStream;
00028 import java.io.FileOutputStream;
00029 import java.io.IOException;
00030 import java.io.InputStream;
00031 import java.io.OutputStream;
00032 import java.util.Enumeration;
00033 import java.util.jar.JarFile;
00034 import java.util.zip.ZipEntry;
00035
00042 public class FileUtils {
00043
00045 public static final int MAX_BUFFER_SIZE = 1024;
00046
00050 private FileUtils() {
00051 }
00052
00063 public static File unpack(JarFile source) throws IOException {
00064 TempRepository tr = TempRepository.getInstance();
00065 File tmp = tr.createDir();
00066
00067 for (Enumeration e = source.entries(); e.hasMoreElements();) {
00068 ZipEntry ze = (ZipEntry) e.nextElement();
00069
00070 if (!ze.getName().endsWith("/")) {
00071 unpack(ze.getName(), source.getInputStream(ze), tmp);
00072 }
00073 }
00074
00075 return tmp;
00076 }
00077
00088 private static void unpack(String name, InputStream is, File root) throws IOException {
00089
00090 String sysfilename = name.replace('/', File.separator.toCharArray()[0]);
00091 String filename = sysfilename.substring(sysfilename.lastIndexOf(File.separator) + 1);
00092 String dirname = sysfilename.substring(0, sysfilename.lastIndexOf(File.separator) + 1);
00093 File dir = new File(root, dirname);
00094
00095 if (!dir.exists()) {
00096 dir.mkdirs();
00097 }
00098
00099
00100 OutputStream os = new FileOutputStream(new File(dir, filename));
00101
00102 fill(is, os);
00103
00104 os.close();
00105 is.close();
00106 }
00107
00116 private static void fill(InputStream is, OutputStream os) throws IOException {
00117 byte[] buffer = new byte[MAX_BUFFER_SIZE];
00118 int read;
00119
00120 while ((read = is.read(buffer, 0, MAX_BUFFER_SIZE)) != -1) {
00121 os.write(buffer, 0, read);
00122 }
00123 }
00124
00133 public static void copy(File source, File target) throws IOException {
00134 InputStream is = new FileInputStream(source);
00135 OutputStream os = new FileOutputStream(new File(target, source.getName()));
00136 fill(is, os);
00137 is.close();
00138 os.close();
00139
00140 }
00141 }