1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.FileInputStream; 27 import java.io.RandomAccessFile; 28 import java.io.BufferedInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.util.Enumeration; 31 import java.util.zip.ZipFile; 32 import java.util.zip.ZipEntry; 33 34 public class XmlZipExtract 35 { 36 37 public static final String CONTENT = "Content.xml"; 38 public static final String OLDCONTENT = "content.xml"; 39 private static final int BUFFER_SIZE = 2048; 40 41 42 /** 43 * Full path of the Zip file to process. 44 */ 45 private String filename = null; 46 47 48 /** 49 * Constructor 50 * 51 * @param filename Full Path to Zip file to process 52 * 53 */ XmlZipExtract(String filename)54 public XmlZipExtract(String filename) { 55 this.filename = filename; 56 } 57 58 /** 59 * Copies Content.xml from zip file onto the filename passed as 60 * an argument 61 * 62 * @param fname Full Path to file to which contents have to be copied 63 * 64 */ getContentXml(String fname)65 public void getContentXml(String fname) throws IOException 66 { 67 try 68 { 69 getContentXmlInt(fname, XmlZipExtract.CONTENT); 70 } 71 catch (NullPointerException e1) 72 { 73 // If the new name of the content file failed, try 74 // the older name. 75 // 76 System.out.println(filename + " Content.xml does not exist, trying content.xml..."); 77 try 78 { 79 getContentXmlInt(fname, XmlZipExtract.OLDCONTENT); 80 } 81 catch (NullPointerException e2) 82 { 83 System.out.println(filename + " content.xml does not exist, trying content.xml..."); 84 throw e2; 85 } 86 } 87 } 88 getContentXmlInt(String fname, String cname)89 public void getContentXmlInt(String fname, String cname) throws IOException 90 { 91 byte b[] = getEntry(cname); 92 93 RandomAccessFile raf=null; 94 raf = new RandomAccessFile(fname, "rw"); 95 raf.write(b); 96 raf.close(); 97 } 98 99 /** 100 * Get the specified entry in the zip file as a stream. 101 * 102 * @param entryName The name of the entry in the zipfile to get. 103 * This should be one of the constants defined above. 104 * 105 * @return byte[] bits for entryName 106 * 107 * @throws IOException if something goes wrong 108 */ getEntry(String entryName)109 public byte[] getEntry(String entryName) throws IOException 110 { 111 ZipFile zf = new ZipFile(filename); 112 ZipEntry ze = zf.getEntry(entryName); 113 byte[] bits = readStream(zf.getInputStream(ze)); 114 zf.close(); 115 return bits; 116 } 117 118 119 /** 120 * Read an InputStream into an array of bytes. 121 * 122 * @param is InputStream of data from Zip file 123 * 124 * @return an array of Bytes 125 */ readStream(InputStream is)126 private byte[] readStream(InputStream is) throws IOException 127 { 128 BufferedInputStream bis = new BufferedInputStream(is); 129 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 130 byte[] buffer = new byte[BUFFER_SIZE]; 131 int eof = 0; 132 while ((eof = bis.read(buffer, 0, buffer.length)) > 0) { 133 baos.write(buffer, 0, eof); 134 } 135 136 return baos.toByteArray(); 137 } 138 } 139 140 141