1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir import java.io.IOException;
29*cdf0e10cSrcweir import java.io.InputStream;
30*cdf0e10cSrcweir import java.io.FileInputStream;
31*cdf0e10cSrcweir import java.io.RandomAccessFile;
32*cdf0e10cSrcweir import java.io.BufferedInputStream;
33*cdf0e10cSrcweir import java.io.ByteArrayOutputStream;
34*cdf0e10cSrcweir import java.util.Enumeration;
35*cdf0e10cSrcweir import java.util.zip.ZipFile;
36*cdf0e10cSrcweir import java.util.zip.ZipEntry;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir public class XmlZipExtract
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir   public static final String CONTENT   = "Content.xml";
42*cdf0e10cSrcweir   public static final String OLDCONTENT   = "content.xml";
43*cdf0e10cSrcweir   private static final int BUFFER_SIZE = 2048;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir   /**
47*cdf0e10cSrcweir    * Full path of the Zip file to process.
48*cdf0e10cSrcweir    */
49*cdf0e10cSrcweir   private String filename = null;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir   /**
53*cdf0e10cSrcweir    * Constructor
54*cdf0e10cSrcweir    *
55*cdf0e10cSrcweir    * @param filename Full Path to Zip file to process
56*cdf0e10cSrcweir    *
57*cdf0e10cSrcweir    */
58*cdf0e10cSrcweir   public XmlZipExtract(String filename) {
59*cdf0e10cSrcweir         this.filename = filename;
60*cdf0e10cSrcweir   }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir   /**
63*cdf0e10cSrcweir    * Copies Content.xml from zip file onto the filename passed as
64*cdf0e10cSrcweir    * an argument
65*cdf0e10cSrcweir    *
66*cdf0e10cSrcweir    * @param fname Full Path to file to which contents have to be copied
67*cdf0e10cSrcweir    *
68*cdf0e10cSrcweir    */
69*cdf0e10cSrcweir   public void getContentXml(String fname) throws IOException
70*cdf0e10cSrcweir   {
71*cdf0e10cSrcweir      try
72*cdf0e10cSrcweir      {
73*cdf0e10cSrcweir         getContentXmlInt(fname, XmlZipExtract.CONTENT);
74*cdf0e10cSrcweir      }
75*cdf0e10cSrcweir      catch (NullPointerException e1)
76*cdf0e10cSrcweir      {
77*cdf0e10cSrcweir         // If the new name of the content file failed, try
78*cdf0e10cSrcweir         // the older name.
79*cdf0e10cSrcweir         //
80*cdf0e10cSrcweir         System.out.println(filename + " Content.xml does not exist, trying content.xml...");
81*cdf0e10cSrcweir         try
82*cdf0e10cSrcweir         {
83*cdf0e10cSrcweir           getContentXmlInt(fname, XmlZipExtract.OLDCONTENT);
84*cdf0e10cSrcweir         }
85*cdf0e10cSrcweir         catch (NullPointerException e2)
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir            System.out.println(filename + " content.xml does not exist, trying content.xml...");
88*cdf0e10cSrcweir            throw e2;
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir      }
91*cdf0e10cSrcweir   }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir   public void getContentXmlInt(String fname, String cname) throws IOException
94*cdf0e10cSrcweir   {
95*cdf0e10cSrcweir      byte b[] = getEntry(cname);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir      RandomAccessFile raf=null;
98*cdf0e10cSrcweir      raf = new RandomAccessFile(fname, "rw");
99*cdf0e10cSrcweir      raf.write(b);
100*cdf0e10cSrcweir      raf.close();
101*cdf0e10cSrcweir   }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir   /**
104*cdf0e10cSrcweir    * Get the specified entry in the zip file as a stream.
105*cdf0e10cSrcweir    *
106*cdf0e10cSrcweir    * @param entryName The name of the entry in the zipfile to get.
107*cdf0e10cSrcweir    *   This should be one of the constants defined above.
108*cdf0e10cSrcweir    *
109*cdf0e10cSrcweir    * @return byte[] bits for entryName
110*cdf0e10cSrcweir    *
111*cdf0e10cSrcweir    * @throws IOException if something goes wrong
112*cdf0e10cSrcweir    */
113*cdf0e10cSrcweir   public byte[] getEntry(String entryName) throws IOException
114*cdf0e10cSrcweir   {
115*cdf0e10cSrcweir         ZipFile zf = new ZipFile(filename);
116*cdf0e10cSrcweir         ZipEntry ze = zf.getEntry(entryName);
117*cdf0e10cSrcweir         byte[] bits = readStream(zf.getInputStream(ze));
118*cdf0e10cSrcweir         zf.close();
119*cdf0e10cSrcweir         return bits;
120*cdf0e10cSrcweir   }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir   /**
124*cdf0e10cSrcweir    * Read an InputStream into an array of bytes.
125*cdf0e10cSrcweir    *
126*cdf0e10cSrcweir    * @param is InputStream of data from Zip file
127*cdf0e10cSrcweir    *
128*cdf0e10cSrcweir    * @return an array of Bytes
129*cdf0e10cSrcweir    */
130*cdf0e10cSrcweir   private byte[] readStream(InputStream is) throws IOException
131*cdf0e10cSrcweir   {
132*cdf0e10cSrcweir         BufferedInputStream bis = new BufferedInputStream(is);
133*cdf0e10cSrcweir         ByteArrayOutputStream baos = new ByteArrayOutputStream();
134*cdf0e10cSrcweir         byte[] buffer = new byte[BUFFER_SIZE];
135*cdf0e10cSrcweir         int eof = 0;
136*cdf0e10cSrcweir         while ((eof = bis.read(buffer, 0, buffer.length)) > 0) {
137*cdf0e10cSrcweir             baos.write(buffer, 0, eof);
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         return baos.toByteArray();
141*cdf0e10cSrcweir   }
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 
145