1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipException ; 33 import java.util.zip.ZipInputStream ; 34 import java.util.zip.ZipOutputStream ; 35 36 44 45 class StoreContentZip { 46 47 protected static final int ZIP_BUFFER = 2048; 48 private long contentLength = 0; 49 private long initialContentLength = -1; 50 private OutputStream theOS = null; 51 52 55 public StoreContentZip() { 56 super(); 57 contentLength = 0; 58 } 59 60 66 67 public void Zip(InputStream inIPS) 68 throws IOException , ZipException { 69 int byteCount = 0; 70 contentLength = 0; 71 initialContentLength = 0; 72 byte data[] = new byte[ZIP_BUFFER]; 73 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 74 ZipOutputStream zoutp = new ZipOutputStream (baos); 75 zoutp.putNextEntry(new ZipEntry ("zippedfile")); 76 while((byteCount = inIPS.read(data,0,ZIP_BUFFER)) != -1 ) { 77 zoutp.write(data,0,byteCount); 78 initialContentLength += byteCount; 79 } 80 zoutp.finish(); 81 zoutp.flush(); 82 zoutp.close(); 83 baos.flush(); 84 baos.close(); 85 contentLength = (long)baos.size(); 86 theOS = baos; 87 } 88 89 95 public void UnZip(InputStream inIPS) 96 throws IOException , ZipException { 97 int byteCount = 0; 98 contentLength = 0; 99 byte indata[] = new byte[ZIP_BUFFER]; 100 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 101 ZipInputStream zinp = new ZipInputStream (inIPS); 102 while (zinp.getNextEntry() != null) { 103 while ((byteCount = zinp.read(indata,0,ZIP_BUFFER)) != -1 ) { 104 baos.write(indata,0,byteCount); 105 } 106 } 107 contentLength = (long)baos.size(); 108 baos.flush(); 109 baos.close(); 110 zinp.close(); 111 theOS = baos; 112 } 113 114 120 public InputStream getInputStream() 121 throws IOException , ZipException { 122 return new ByteArrayInputStream ( 123 ((ByteArrayOutputStream )theOS).toByteArray()); 124 } 125 126 132 public OutputStream getOutputStream() 133 throws IOException , ZipException { 134 return theOS; 135 } 136 137 141 public long getContentLength() { 142 return contentLength; 143 } 144 145 public long getInitialContentLength() { 146 return initialContentLength; 147 } 148 149 } 150 | Popular Tags |