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