001 /** 002 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved, 003 * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of version 2 of the GNU General Public License as 006 * published by the Free Software Foundation. 007 * There are special exceptions to the terms and conditions of the GPL 008 * as it is applied to this software. See the GNU General Public License for more details. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 package org.dbreplicator.replication.zip; 021 022 import java.io.*; 023 import java.util.zip.*; 024 025 import org.dbreplicator.replication.*; 026 027 /** 028 * This class is used to make zip file of the XML file,BLOB.lob and CLOB.lob 029 * so that compacted files can be transferred over the network. Besides it, this 030 * class contains method for unzipping it. 031 */ 032 033 public class ZipHandler 034 { 035 036 /** 037 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL> 038 * @param zipURL 039 * @param xmlURL 040 * @param xmlFileName 041 */ 042 043 044 public static void makeZip(String zipURL, String xmlURL, String xmlFileName) throws 045 IOException 046 { 047 FileOutputStream fos = new FileOutputStream(new File(zipURL)); 048 ZipOutputStream zos = new ZipOutputStream(fos); 049 // zos.setLevel(); 050 FileInputStream fis = new FileInputStream(xmlURL); 051 zos.putNextEntry(new ZipEntry(xmlFileName+".xml")); 052 writeInOutputStream(fis,zos); 053 String bpath = PathHandler.getBLobFilePathForClient(); 054 FileInputStream fisBLOB = createInputStream(bpath); 055 zos.putNextEntry(new ZipEntry("blob.lob")); 056 writeInOutputStream(fisBLOB, zos); 057 058 zos.closeEntry(); 059 String cpath = PathHandler.getCLobFilePathForClient(); 060 FileInputStream fisCLOB = createInputStream(cpath); 061 zos.putNextEntry(new ZipEntry("clob.lob")); 062 writeInOutputStream(fisCLOB, zos); 063 zos.closeEntry(); 064 fis.close(); 065 fisCLOB.close(); 066 fisBLOB.close(); 067 zos.close(); 068 fos.close(); 069 070 } 071 072 073 /** 074 * unzipps a zip file placed at <zipURL> to path <xmlURL> 075 * @param zipURL 076 * @param xmlURL 077 */ 078 public static void unZip(String zipURL, String xmlURL) throws IOException 079 { 080 FileOutputStream fosBLOB = new FileOutputStream(PathHandler.getBLobFilePathForClient()); 081 FileOutputStream fosCLOB = new FileOutputStream(PathHandler.getCLobFilePathForClient()); 082 FileInputStream fis = new FileInputStream(new File(zipURL)); 083 ZipInputStream zis = new ZipInputStream(fis); 084 FileOutputStream fos = new FileOutputStream(xmlURL); 085 ZipEntry ze = zis.getNextEntry(); 086 ExtractZip(zis, fos, fosBLOB, fosCLOB, ze); 087 ze = zis.getNextEntry(); 088 ExtractZip(zis, fos, fosBLOB, fosCLOB, ze); 089 ze = zis.getNextEntry(); 090 ExtractZip(zis, fos, fosBLOB, fosCLOB, ze); 091 fos.flush(); 092 fis.close(); 093 fos.close(); 094 fosCLOB.close(); 095 fosBLOB.close(); 096 zis.close(); 097 } 098 099 private static void ExtractZip(ZipInputStream zis, FileOutputStream fos, 100 FileOutputStream fosBLOB, 101 FileOutputStream fosCLOB, ZipEntry ze) throws 102 IOException { 103 if (ze.getName().equalsIgnoreCase("blob.lob")) 104 writeInOutputStream(zis, fosBLOB); 105 else if (ze.getName().equalsIgnoreCase("clob.lob")) 106 writeInOutputStream(zis, fosCLOB); 107 else 108 writeInOutputStream(zis, fos); 109 } 110 111 private static void writeInOutputStream(InputStream is, 112 OutputStream os) throws 113 IOException { 114 byte[] buf = new byte[1024]; 115 int len = 0; 116 while ( (len = is.read(buf)) > 0) 117 { 118 os.write(buf, 0, len); 119 } 120 } 121 122 /** 123 * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL> 124 * @param zipURL 125 * @param xmlURL 126 * @param xmlFileName 127 */ 128 129 public static void makeStructZip(String zipURL, String xmlURL, String xmlFileName) throws 130 IOException 131 { 132 FileOutputStream fos = new FileOutputStream(new File(zipURL)); 133 ZipOutputStream zos = new ZipOutputStream(fos); 134 // zos.setLevel(); 135 FileInputStream fis = new FileInputStream(xmlURL); 136 zos.putNextEntry(new ZipEntry(xmlFileName+".xml")); 137 writeInOutputStream(fis,zos); 138 zos.closeEntry(); 139 fis.close(); 140 zos.close(); 141 } 142 143 /** 144 * unzipps a zip file placed at <zipURL> to path <xmlURL> 145 * @param zipURL 146 * @param xmlURL 147 */ 148 public static void unStructZip(String zipURL, String xmlURL) throws IOException 149 { 150 FileInputStream fis = new FileInputStream(new File(zipURL)); 151 ZipInputStream zis = new ZipInputStream(fis); 152 FileOutputStream fos = new FileOutputStream(xmlURL); 153 ZipEntry ze = zis.getNextEntry(); 154 writeInOutputStream(zis,fos); 155 fos.flush(); 156 fis.close(); 157 fos.close(); 158 zis.close(); 159 } 160 161 private static FileInputStream createInputStream(String bpath) throws IOException{ 162 FileInputStream fis = null; 163 try 164 { 165 fis = new FileInputStream(bpath); 166 } 167 catch (FileNotFoundException ex) 168 { 169 FileOutputStream temp = new FileOutputStream(bpath); 170 fis = new FileInputStream(bpath); 171 } 172 return fis; 173 } 174 }

