Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package edu.tum.cs.ias.knowrob.utils;
00011
00012 import java.io.BufferedInputStream;
00013 import java.io.BufferedOutputStream;
00014 import java.io.File;
00015 import java.io.FileNotFoundException;
00016 import java.io.FileOutputStream;
00017 import java.io.FileReader;
00018 import java.io.IOException;
00019 import java.util.Enumeration;
00020 import java.util.regex.Matcher;
00021 import java.util.zip.ZipEntry;
00022 import java.util.zip.ZipFile;
00023
00030 public class FileUtil {
00031
00044 public static String getAbsoluteFilePath(String root, String path) {
00045 File file = new File(path);
00046 if (file.isAbsolute())
00047 return file.getAbsolutePath();
00048
00049 if (root == null)
00050 return null;
00051
00052 return new File(new File(root), path).getAbsolutePath();
00053 }
00054
00066 public static String readTextFile(File file) throws FileNotFoundException, IOException {
00067 FileReader fr = new FileReader(file);
00068 char[] cbuf = new char[(int) file.length()];
00069 fr.read(cbuf);
00070 String content = new String(cbuf);
00071 fr.close();
00072 return content;
00073 }
00074
00087 public static String readTextFile(String filename) throws FileNotFoundException, IOException {
00088 return readTextFile(new File(filename));
00089 }
00090
00100 public static boolean Unzip(String zipFile, String outputDirectory) {
00101 String outDir = outputDirectory;
00102 if (!outDir.endsWith("/") && !outDir.endsWith("\\"))
00103 outDir += File.separator;
00104
00105 BufferedOutputStream dest = null;
00106 BufferedInputStream is = null;
00107 int BUFFER = 2048;
00108 ZipEntry entry;
00109 ZipFile zipfile;
00110 try {
00111 zipfile = new ZipFile(zipFile);
00112 Enumeration<? extends ZipEntry> e = zipfile.entries();
00113 while (e.hasMoreElements()) {
00114 entry = e.nextElement();
00115 if (entry.isDirectory()) {
00116 (new File(outDir + entry.getName())).mkdir();
00117 continue;
00118 }
00119
00120 String name;
00121
00122 if (File.separator.equals("\\")) {
00123 name = entry.getName()
00124 .replaceAll("/", Matcher.quoteReplacement(File.separator));
00125 } else {
00126 name = entry.getName().replaceAll("\\\\",
00127 Matcher.quoteReplacement(File.separator));
00128 }
00129
00130 String filename = outDir + name;
00131 String filePath = filename.substring(0, filename.lastIndexOf(File.separator));
00132
00133
00134 if (!(new File(filePath)).exists()) {
00135 (new File(filePath)).mkdirs();
00136 }
00137 is = new BufferedInputStream(zipfile.getInputStream(entry));
00138 int count;
00139 byte data[] = new byte[BUFFER];
00140 FileOutputStream fos = new FileOutputStream(filename);
00141 dest = new BufferedOutputStream(fos, BUFFER);
00142 while ((count = is.read(data, 0, BUFFER)) != -1) {
00143 dest.write(data, 0, count);
00144 }
00145 dest.close();
00146 is.close();
00147 }
00148 zipfile.close();
00149 } catch (IOException e1) {
00150
00151 System.err.println("Couldn't unzip file: " + zipFile);
00152 e1.printStackTrace();
00153 return false;
00154 }
00155 return true;
00156 }
00157 }