1 25 26 package com.j2biz.blogunity.util; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.text.DecimalFormat ; 36 import java.util.zip.ZipEntry ; 37 import java.util.zip.ZipInputStream ; 38 import java.util.zip.ZipOutputStream ; 39 40 import org.apache.commons.lang.StringUtils; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 import com.j2biz.blogunity.BlogunityManager; 45 import com.j2biz.blogunity.exception.BlogunityException; 46 import com.j2biz.blogunity.i18n.I18N; 47 import com.j2biz.blogunity.i18n.I18NStatusFactory; 48 49 56 public class ResourceUtils { 57 60 private static final Log log = LogFactory.getLog(ResourceUtils.class); 61 62 private static final long KB = 1024; 63 64 private static final long MB = 1024 * 1024; 65 66 private static final long GB = 1024 * 1024 * 1024; 67 68 private static DecimalFormat FILESIZE_FORMATER = new DecimalFormat ("####.##"); 69 70 75 public static InputStream getResourceAsStream(String url) throws BlogunityException { 76 if (log.isDebugEnabled()) { 77 log.debug("Getting InputStream for requested resource url=" + url); 78 } 79 80 if (url.charAt(0) != '/') url = "/" + url; 81 82 String mainDataDirectory = BlogunityManager.getSystemConfiguration().getDataDir(); 83 84 File f = new File (mainDataDirectory, url); 85 if (f.exists() && f.isFile() && f.canRead()) { 86 FileInputStream in; 87 try { 88 in = new FileInputStream (f); 89 return in; 90 } catch (FileNotFoundException e) { 91 log.error("getThemeResourceAsStream(String)", e); 92 throw new BlogunityException(I18NStatusFactory.create( 93 I18N.ERRORS.RESOURCE_NOT_FOUND, url)); 94 } 95 96 } 97 98 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.RESOURCE_NOT_FOUND, url)); 99 } 100 101 public static synchronized void copyDirectory(File source, File destination) 102 throws BlogunityException { 103 104 if (source.isDirectory()) { 105 if (!destination.exists()) { 106 destination.mkdir(); 107 } 108 109 String [] children = source.list(); 110 for (int i = 0; i < children.length; i++) { 111 copyDirectory(new File (source, children[i]), new File (destination, children[i])); 112 } 113 } else { 114 copyFile(source, destination); 115 } 116 117 } 118 119 public static synchronized void copyFile(File source, File destination) 120 throws BlogunityException { 121 try { 122 InputStream in = new FileInputStream (source); 123 OutputStream out = new FileOutputStream (destination); 124 125 byte[] buf = new byte[1024]; 127 int len; 128 while ((len = in.read(buf)) > 0) { 129 out.write(buf, 0, len); 130 } 131 in.close(); 132 out.close(); 133 } catch (Throwable t) { 134 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 135 } 136 } 137 138 public static synchronized void copyFile(InputStream in, File destination) 139 throws BlogunityException { 140 try { 141 OutputStream out = new FileOutputStream (destination); 142 143 byte[] buf = new byte[1024]; 145 int len; 146 while ((len = in.read(buf)) > 0) { 147 out.write(buf, 0, len); 148 } 149 in.close(); 150 out.close(); 151 } catch (Throwable t) { 152 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 153 } 154 } 155 156 public static synchronized File zipDirectory(File sourceDirectory) throws BlogunityException { 157 return zipDirectory(sourceDirectory, sourceDirectory.getName()); 158 } 159 160 public static synchronized File zipDirectory(File sourceDirectory, String rootDirName) 161 throws BlogunityException { 162 163 try { 164 File zippedFile = new File (BlogunityManager.getSystemConfiguration().getTempDir(), 165 sourceDirectory.getName() + System.currentTimeMillis() + ".zip"); 166 167 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (zippedFile)); 168 169 ZipEntry anEntry = new ZipEntry (rootDirName); 172 zipDir(sourceDirectory, zos, sourceDirectory.getAbsolutePath(), rootDirName); 173 zos.close(); 174 175 return zippedFile; 176 } catch (Throwable t) { 177 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 178 } 179 180 } 181 182 private static synchronized void zipDir(File zipDir, ZipOutputStream zos, 183 String absolutePathToThemeDir, String rootDirName) throws IOException { 184 185 String [] dirList = zipDir.list(); 187 byte[] readBuffer = new byte[2156]; 188 int bytesIn = 0; 189 for (int i = 0; i < dirList.length; i++) { 191 File f = new File (zipDir, dirList[i]); 192 if (f.isDirectory()) { 193 zipDir(f, zos, absolutePathToThemeDir, rootDirName); 194 continue; 195 } 196 197 FileInputStream fis = new FileInputStream (f); 200 201 String path = (StringUtils.isNotEmpty(rootDirName) ? rootDirName : "") 203 + "/" 204 + f.getAbsolutePath().substring(absolutePathToThemeDir.length() + 1, 205 f.getAbsolutePath().length()); 206 207 ZipEntry anEntry = new ZipEntry (path); 208 zos.putNextEntry(anEntry); 210 211 while ((bytesIn = fis.read(readBuffer)) != -1) { 213 zos.write(readBuffer, 0, bytesIn); 214 } 215 216 fis.close(); 218 } 219 } 220 221 public static synchronized void removeDirectory(File dir) throws BlogunityException { 222 if (dir.isDirectory()) { 223 String [] children = dir.list(); 224 for (int i = 0; i < children.length; i++) { 225 removeDirectory(new File (dir, children[i])); 226 } 227 } 228 229 removeFile(dir); 230 } 231 232 public static synchronized void removeFile(File file) throws BlogunityException { 233 boolean result = file.delete(); 234 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 235 I18N.ERRORS.DELETE_FILE, file.getAbsolutePath())); } 236 237 } 238 239 public static synchronized void unzipFile(File zipFile, File destinationDir) 240 throws BlogunityException { 241 try { 242 ZipInputStream in = new ZipInputStream (new FileInputStream (zipFile)); 243 unzipFile(in, destinationDir); 244 in.close(); 245 } catch (Throwable t) { 246 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 247 } 248 } 249 250 public static synchronized void unzipFile(ZipInputStream zin, File destinationDir) 251 throws BlogunityException { 252 253 try { 254 ZipEntry e; 255 while ((e = zin.getNextEntry()) != null) { 256 if (e.isDirectory()) { 257 new File (destinationDir, e.getName()).mkdir(); 259 } else { 260 String tempName = e.getName(); 266 int indx = tempName.lastIndexOf("/"); 267 if (indx > 0) { 268 String dirs = tempName.substring(0, indx); 269 new File (destinationDir, dirs).mkdirs(); 270 } else 271 indx = 0; 272 273 File f = new File (destinationDir, e.getName()); 274 f.createNewFile(); 275 unzipEntry(zin, f); 276 } 277 } 278 zin.close(); 279 } catch (Throwable t) { 280 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 281 } 282 283 } 284 285 private static synchronized void unzipEntry(ZipInputStream zin, File outFile) 286 throws IOException { 287 FileOutputStream out = new FileOutputStream (outFile); 288 byte[] buf = new byte[1024]; 289 int len; 290 while ((len = zin.read(buf)) > 0) { 291 out.write(buf, 0, len); 292 } 293 out.close(); 294 } 295 296 public static String getPreformattedFilesize(long size) { 297 298 if (size < KB) { return size + " Bytes"; } 299 300 if (size < MB) { 301 double kbytes = (double) size / KB; 302 return FILESIZE_FORMATER.format(kbytes) + " KB"; 303 304 } 305 306 if (size < GB) { 307 double gbytes = (double) size / MB; 308 return FILESIZE_FORMATER.format(gbytes) + " MB"; 309 310 } 311 312 double gbytes = (double) size / GB; 313 return FILESIZE_FORMATER.format(gbytes) + " GB"; 314 315 } 316 317 } | Popular Tags |