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