1 11 12 23 24 27 package org.openuss.utility; 28 29 import java.io.*; 30 31 import java.util.zip.*; 32 33 34 public class Uncompress { 35 38 public static void gunzipFile(String from, String to) 39 throws IllegalArgumentException , IOException { 40 byte[] buffer = new byte[4096]; 41 int bytes_read; 42 43 File fin = new File(from); 45 46 if (!fin.exists()) { 47 throw new IllegalArgumentException (from + " does not exist."); 48 } 49 50 if (!fin.canRead()) { 51 throw new IllegalArgumentException (from + " read protected."); 52 } 53 54 GZIPInputStream in = new GZIPInputStream(new FileInputStream(from)); 56 57 File fout = new File(to); 58 59 if (fout.exists()) { 60 throw new IllegalArgumentException ("File '" + to + 62 "' already exisits."); 63 } 64 65 FileOutputStream out = new FileOutputStream(to); 66 67 while ((bytes_read = in.read(buffer)) != -1) 68 69 out.write(buffer, 0, bytes_read); 71 72 in.close(); 73 out.close(); 74 } 75 76 79 public static void unzipFile(String from, String to) 80 throws IllegalArgumentException , IOException { 81 byte[] buffer = new byte[4096]; 82 int bytes_read; 83 84 File fin = new File(from); 86 87 if (!fin.exists()) { 88 throw new IllegalArgumentException (from + " does not exist."); 89 } 90 91 if (!fin.canRead()) { 92 throw new IllegalArgumentException (from + " read protected."); 93 } 94 95 ZipInputStream in = new ZipInputStream(new FileInputStream(from)); 96 ZipEntry entry; 97 FileOutputStream out; 98 99 while ((entry = in.getNextEntry()) != null) { 100 String toName = to + File.separator + entry.getName(); 101 File fout = new File(toName); 102 103 114 if (entry.isDirectory()) { 115 if (!fout.mkdirs()) { 117 System.err.println("Unable to create directory: " + 118 toName); 119 } 120 } else { 121 out = new FileOutputStream(toName); 123 124 while ((bytes_read = in.read(buffer)) != -1) 125 out.write(buffer, 0, bytes_read); 126 127 out.close(); 128 } 129 130 } 132 133 in.close(); 134 } 135 } | Popular Tags |