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