Compression.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package commons.utility;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.util.Enumeration;
  11. import java.util.zip.DataFormatException;
  12. import java.util.zip.Deflater;
  13. import java.util.zip.Inflater;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipFile;
  16. import java.util.zip.ZipOutputStream;
  17. import commons.utility.database.NullValue;
  18. import commons.exception.ZCompressException;
  19. public class Compression {
  20. /** Factory per la creazione dei file in uscita
  21. */
  22. public final static class OutputZipFileFactory
  23. {
  24. /** Crea un file zip aperto in scrittura a partire da un OutputStream
  25. * @param outstream lo stream su cui wrappare lo zip
  26. * @param sizeBuffer se != null && > 0 allora lo stream viene wrappato all'interno di un BufferedOutputStream
  27. * @return Una impl wrappata nel seguente modo:
  28. * se outstream instanceof ZipOutputStream wrap diretto su outstream
  29. * se outstream instanceof BufferedOutputStream allora wrap di un ZipOutputStream intorno a outstrema
  30. * negli altri casi,se sizeBuffer > 0 allora wrapping su un BufferedOutputStream(outstream,sizeBuffer)
  31. * altrimenti wrapping diretto su outstream
  32. * @throws ZCompressException
  33. */
  34. public final static OutputZipFile create(final OutputStream outstream,final Integer sizeBuffer) throws ZCompressException
  35. {
  36. OutputZipFile impl = null;
  37. // Se lo stream ? quello di un file zip...
  38. if(outstream instanceof ZipOutputStream)
  39. // ...aggancialo e basta!
  40. impl = new OutputZipFile((ZipOutputStream) outstream);
  41. else
  42. {
  43. OutputStream _outstream = outstream;
  44. if(null != sizeBuffer && sizeBuffer.intValue() > 0 && !(outstream instanceof BufferedOutputStream))
  45. _outstream = new BufferedOutputStream(outstream,sizeBuffer);
  46. impl = new OutputZipFile
  47. (
  48. new ZipOutputStream(_outstream)
  49. );
  50. }
  51. return impl;
  52. }
  53. /** Crea un file zip aperto in scrittura,se il file esiste viene sovrascritto
  54. * @param filename
  55. * @return
  56. * @throws ZCompressException
  57. */
  58. public final static OutputZipFile create(String filename) throws ZCompressException
  59. {
  60. FileOutputStream fos = null;
  61. try
  62. {
  63. fos = new FileOutputStream(filename);
  64. return create(fos,10);
  65. }
  66. catch(IOException e)
  67. {
  68. if(null != fos)
  69. {
  70. try
  71. {
  72. fos.close();
  73. }
  74. catch (IOException e1)
  75. {
  76. }
  77. fos = null;
  78. }
  79. throw new ZCompressException(e);
  80. }
  81. finally
  82. {
  83. }
  84. }
  85. }
  86. public Compression() {}
  87. private static final int BUFFER= 2048;
  88. /////////////////////////////////////////////////////////////////////////////
  89. //Compressione Decompressione FILE
  90. /** Comprime un blocco di dati all'interno di un file zip
  91. * @param dataToStore il blocco di dati da comprimere
  92. * @param dataToStoreName il nome che il blocco di dati avr? all'interno del file zip
  93. * @param outputFile il nome del file di uscita
  94. * @throws ZCompressException
  95. */
  96. public static void CompressFile(byte[] dataToStore, String dataToStoreName, String outputFile) throws ZCompressException
  97. {
  98. ZipOutputStream out = null;
  99. try
  100. {
  101. out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
  102. ZipEntry entry = new ZipEntry(dataToStoreName);
  103. out.putNextEntry(entry);
  104. out.write(dataToStore);
  105. }
  106. catch(IOException e)
  107. {
  108. throw new ZCompressException(e);
  109. }
  110. finally
  111. {
  112. try
  113. {
  114. if(null != out) out.close();
  115. }
  116. catch(IOException e)
  117. {
  118. throw new ZCompressException(e);
  119. }
  120. }
  121. }
  122. public static String CompressFile(String inputFile, String outputFile) throws ZCompressException {
  123. try
  124. {
  125. String err= "OK";
  126. BufferedInputStream origin= null;
  127. ZipOutputStream out= null;
  128. FileInputStream fi= null;
  129. try {
  130. FileOutputStream dest= new FileOutputStream(outputFile);
  131. out= new ZipOutputStream(new BufferedOutputStream(dest));
  132. byte data[]= new byte[BUFFER];
  133. File f= new File(inputFile);
  134. fi= new FileInputStream(f);
  135. origin= new BufferedInputStream(fi, BUFFER);
  136. ZipEntry entry= new ZipEntry(f.getName());
  137. out.putNextEntry(entry);
  138. int count;
  139. while ((count= origin.read(data, 0, BUFFER)) != -1) {
  140. out.write(data, 0, count);
  141. }
  142. } catch (IOException ex) {
  143. err= ex.getMessage();
  144. } finally {
  145. origin.close();
  146. out.close();
  147. fi.close();
  148. }
  149. return err;
  150. }
  151. catch (IOException e)
  152. {
  153. throw new ZCompressException(e);
  154. }
  155. }
  156. public static String CompressFile(String[] inputFile, String outputFile) throws ZCompressException {
  157. try
  158. {
  159. String err= "OK";
  160. BufferedInputStream origin= null;
  161. ZipOutputStream out= null;
  162. FileInputStream fi= null;
  163. try {
  164. FileOutputStream dest= new FileOutputStream(outputFile);
  165. out= new ZipOutputStream(new BufferedOutputStream(dest));
  166. byte data[]= new byte[BUFFER];
  167. for (int i= 0; i < inputFile.length; i++) {
  168. File f= new File(inputFile[i]);
  169. fi= new FileInputStream(f);
  170. origin= new BufferedInputStream(fi, BUFFER);
  171. ZipEntry entry= new ZipEntry(f.getName());
  172. out.putNextEntry(entry);
  173. int count;
  174. while ((count= origin.read(data, 0, BUFFER)) != -1) {
  175. out.write(data, 0, count);
  176. }
  177. fi.close();
  178. }
  179. } catch (IOException ex) {
  180. err= ex.getMessage();
  181. } finally {
  182. if (origin != null) {
  183. origin.close();
  184. }
  185. if (out != null) {
  186. out.close();
  187. }
  188. if (fi != null) {
  189. fi.close();
  190. }
  191. }
  192. return err;
  193. }
  194. catch (IOException e)
  195. {
  196. throw new ZCompressException(e);
  197. }
  198. }
  199. public static String DecompressFile(String inputFile, String outputFile) throws ZCompressException {
  200. try
  201. {
  202. String err= "OK";
  203. BufferedOutputStream dest= null;
  204. BufferedInputStream is= null;
  205. FileOutputStream fos= null;
  206. ZipFile zipfile= null;
  207. try {
  208. ZipEntry entry;
  209. zipfile= new ZipFile(inputFile);
  210. Enumeration e= zipfile.entries();
  211. while (e.hasMoreElements()) {
  212. entry= (ZipEntry)e.nextElement();
  213. is= new BufferedInputStream(zipfile.getInputStream(entry));
  214. int count;
  215. byte data[]= new byte[BUFFER];
  216. if (null == outputFile || NullValue.STRING.equalsIgnoreCase(outputFile))
  217. {
  218. String path = inputFile.substring(0, inputFile.lastIndexOf(File.separator));
  219. fos= new FileOutputStream(path + File.separator + entry.getName());
  220. }
  221. else
  222. {
  223. fos= new FileOutputStream(outputFile);
  224. }
  225. dest= new BufferedOutputStream(fos, BUFFER);
  226. while ((count= is.read(data, 0, BUFFER)) != -1) {
  227. dest.write(data, 0, count);
  228. }
  229. if(dest != null)
  230. {
  231. dest.flush();
  232. dest.close();
  233. }
  234. if(is != null)
  235. {
  236. is.close();
  237. }
  238. if(fos != null)
  239. {
  240. fos.flush();
  241. fos.close();
  242. }
  243. }
  244. } catch (IOException ex) {
  245. err= ex.getMessage();
  246. } finally {
  247. if(dest != null)
  248. {
  249. dest.flush();
  250. dest.close();
  251. }
  252. if(is != null)
  253. {
  254. is.close();
  255. }
  256. if(fos != null)
  257. {
  258. fos.flush();
  259. fos.close();
  260. }
  261. if(zipfile != null)
  262. {
  263. zipfile.close();
  264. }
  265. }
  266. return err;
  267. }
  268. catch (IOException e)
  269. {
  270. throw new ZCompressException(e);
  271. }
  272. }
  273. public static String DecompressFile(String inputFile) throws ZCompressException {
  274. return DecompressFile(inputFile, null);
  275. }
  276. /////////////////////////////////////////////////////////////////////////////
  277. //Compressione Decompressione BYTE[]
  278. public static byte[] Compress(byte[] input) throws ZCompressException {
  279. try
  280. {
  281. Deflater compressor= new Deflater();
  282. compressor.setLevel(Deflater.BEST_COMPRESSION);
  283. // Give the compressor the data to compress
  284. compressor.setInput(input);
  285. compressor.finish();
  286. ByteArrayOutputStream bos= new ByteArrayOutputStream(input.length);
  287. // Compress the data
  288. byte[] buf= new byte[BUFFER];
  289. while (!compressor.finished()) {
  290. int count= compressor.deflate(buf);
  291. bos.write(buf, 0, count);
  292. }
  293. //try {
  294. bos.close();
  295. //} catch (IOException e){
  296. //}
  297. // Get the compressed data
  298. byte[] compressedData= bos.toByteArray();
  299. return compressedData;
  300. }
  301. catch (IOException e)
  302. {
  303. throw new ZCompressException(e);
  304. }
  305. }
  306. public static byte[] Decompress(byte[] input) throws ZCompressException {
  307. try
  308. {
  309. Inflater decompressor= new Inflater();
  310. decompressor.setInput(input);
  311. // Create an expandable byte array to hold the decompressed data
  312. ByteArrayOutputStream bos= new ByteArrayOutputStream(input.length);
  313. // Decompress the data
  314. byte[] buf= new byte[BUFFER];
  315. while (!decompressor.finished()) {
  316. //try {
  317. int count= decompressor.inflate(buf);
  318. bos.write(buf, 0, count);
  319. //} catch (DataFormatException e) {
  320. //}
  321. }
  322. //try {
  323. bos.close();
  324. //} catch (IOException e) {
  325. //}
  326. // Get the decompressed data
  327. byte[] decompressedData= bos.toByteArray();
  328. return decompressedData;
  329. }
  330. catch (DataFormatException e)
  331. {
  332. throw new ZCompressException(e);
  333. }
  334. catch (IOException e)
  335. {
  336. throw new ZCompressException(e);
  337. }
  338. }
  339. public static void CompressFile(String pathRedix, String pathSingleFile, ZipOutputStream out) throws IOException {
  340. BufferedInputStream origin = null;
  341. FileInputStream fi = null;
  342. try {
  343. byte data[] = new byte[BUFFER];
  344. File f = new File(pathRedix + pathSingleFile);
  345. fi = new FileInputStream(f);
  346. origin = new BufferedInputStream(fi, BUFFER);
  347. ZipEntry entry = new ZipEntry(pathSingleFile);
  348. out.putNextEntry(entry);
  349. int count;
  350. while ((count = origin.read(data, 0, BUFFER)) != -1) {
  351. out.write(data, 0, count);
  352. }
  353. fi.close();
  354. } catch (IOException ex) {
  355. throw ex;
  356. } finally {
  357. if (origin != null) {
  358. origin.close();
  359. origin = null;
  360. }
  361. if (fi != null) {
  362. fi.close();
  363. fi = null;
  364. }
  365. }
  366. }
  367. }