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.util.List; 29*cdf0e10cSrcweir import java.util.ListIterator; 30*cdf0e10cSrcweir import java.util.LinkedList; 31*cdf0e10cSrcweir import java.util.zip.ZipInputStream; 32*cdf0e10cSrcweir import java.util.zip.ZipOutputStream; 33*cdf0e10cSrcweir import java.util.zip.ZipEntry; 34*cdf0e10cSrcweir import java.util.zip.CRC32; 35*cdf0e10cSrcweir import java.io.InputStream; 36*cdf0e10cSrcweir import java.io.OutputStream; 37*cdf0e10cSrcweir import java.io.IOException; 38*cdf0e10cSrcweir import java.io.ByteArrayOutputStream; 39*cdf0e10cSrcweir import org.openoffice.xmerge.util.Debug; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir /** 42*cdf0e10cSrcweir * Class used by OfficeDocument to handle zip reading and writing, 43*cdf0e10cSrcweir * as well as storing zip entries. 44*cdf0e10cSrcweir * 45*cdf0e10cSrcweir * @author Herbie Ong 46*cdf0e10cSrcweir */ 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir class OfficeZip { 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir /** file name of the xml file in a zipped document. */ 51*cdf0e10cSrcweir private final static String XMLFILE = "content.xml"; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir private final static int BUFFERSIZE = 1024; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir private List entryList = null; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir private int contentIndex = -1; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir private String filename = null; 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir private class Entry { 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir ZipEntry zipEntry = null; 64*cdf0e10cSrcweir byte bytes[] = null; 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir /** 68*cdf0e10cSrcweir * Constructor 69*cdf0e10cSrcweir * 70*cdf0e10cSrcweir * @param filename Full Path to Zip file to process 71*cdf0e10cSrcweir * 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir public OfficeZip(String filename) { 74*cdf0e10cSrcweir this.filename = filename; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir /** 79*cdf0e10cSrcweir * Read each zip entry in the given InputStream object 80*cdf0e10cSrcweir * and store in entryList both the ZipEntry object as well 81*cdf0e10cSrcweir * as the bits of each entry. Return the bytes for the 82*cdf0e10cSrcweir * entry of XMLFILE. 83*cdf0e10cSrcweir * 84*cdf0e10cSrcweir * @param is InputStream object to read from 85*cdf0e10cSrcweir * @return byte[] byte array of XML file 86*cdf0e10cSrcweir * @throws IOException if any I/O error occurs 87*cdf0e10cSrcweir */ 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir byte[] read(InputStream is) throws IOException { 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir ZipInputStream zis = new ZipInputStream(is); 92*cdf0e10cSrcweir ZipEntry ze = null; 93*cdf0e10cSrcweir int i = -1; 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir entryList = new LinkedList(); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir while ((ze = zis.getNextEntry()) != null) { 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir String name = ze.getName(); 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir Entry entry = new Entry(); 102*cdf0e10cSrcweir entry.zipEntry = ze; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir Debug.log(Debug.TRACE, "reading entry: " + name); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir ByteArrayOutputStream baos = new ByteArrayOutputStream(); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir int len = 0; 109*cdf0e10cSrcweir byte bytes[] = new byte[BUFFERSIZE]; 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir while ((len = zis.read(bytes)) > 0) { 112*cdf0e10cSrcweir baos.write(bytes, 0, len); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir entry.bytes = baos.toByteArray(); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir entryList.add(entry); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir i++; 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir if (isContentXML(name)) { 122*cdf0e10cSrcweir contentIndex = i; 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir if (contentIndex == -1) { 127*cdf0e10cSrcweir throw new IOException(XMLFILE + " not found."); 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir Entry contentEntry = (Entry) entryList.get(contentIndex); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir return contentEntry.bytes; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /** 136*cdf0e10cSrcweir * Write out the XMLFILE as a zip into the OutputStream object. 137*cdf0e10cSrcweir * 138*cdf0e10cSrcweir * If a zip inputstream was previously read, then use 139*cdf0e10cSrcweir * those zip contents to recreate the zip, except for XMLFILE, 140*cdf0e10cSrcweir * update it using the new content from xmlBytes. 141*cdf0e10cSrcweir * 142*cdf0e10cSrcweir * If there was no zip inputstream previously read, write 143*cdf0e10cSrcweir * XMLFILE out into the zip outputstream. 144*cdf0e10cSrcweir * 145*cdf0e10cSrcweir * @param os OutputStream object to write zip 146*cdf0e10cSrcweir * @param xmlBytes bytes of XMLFILE 147*cdf0e10cSrcweir * @throws IOException if any I/O errors occur. 148*cdf0e10cSrcweir */ 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir void write(OutputStream os, byte xmlBytes[]) throws IOException { 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir ZipOutputStream zos = new ZipOutputStream(os); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir // if read was not invoked previously, store the bytes directly. 155*cdf0e10cSrcweir if (contentIndex == -1) { 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir Debug.log(Debug.TRACE, "Writing out " + XMLFILE + " into zip."); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir ZipEntry ze = new ZipEntry(XMLFILE); 160*cdf0e10cSrcweir ze.setSize(xmlBytes.length); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir CRC32 crc = new CRC32(); 163*cdf0e10cSrcweir crc.reset(); 164*cdf0e10cSrcweir crc.update(xmlBytes); 165*cdf0e10cSrcweir ze.setCrc(crc.getValue()); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir ze.setTime(System.currentTimeMillis()); 168*cdf0e10cSrcweir ze.setMethod(ZipEntry.DEFLATED); 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir zos.putNextEntry(ze); 171*cdf0e10cSrcweir zos.write(xmlBytes); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir } else { 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir saveEntries(zos, xmlBytes); 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir zos.close(); 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir /** 182*cdf0e10cSrcweir * Used by write method if there was a zip inputstream 183*cdf0e10cSrcweir * previously read. It would write out each ZipEntry of 184*cdf0e10cSrcweir * the previously read zip, except for XMLFILE, it would 185*cdf0e10cSrcweir * update it with new values and with the content from 186*cdf0e10cSrcweir * xmlBytes. 187*cdf0e10cSrcweir * 188*cdf0e10cSrcweir * @param os OutputStream object to write zip 189*cdf0e10cSrcweir * @param xmlBytes bytes of XMLFILE 190*cdf0e10cSrcweir * @throws ZipException if any zip I/O errors occur. 191*cdf0e10cSrcweir */ 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir private void saveEntries(ZipOutputStream zos, byte xmlBytes[]) 194*cdf0e10cSrcweir throws IOException { 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir Debug.log(Debug.TRACE, "Writing out the following entries into zip."); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir ListIterator iterator = entryList.listIterator(); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir while (iterator.hasNext()) { 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir Entry entry = (Entry) iterator.next(); 203*cdf0e10cSrcweir ZipEntry ze = entry.zipEntry; 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir String name = ze.getName(); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir Debug.log(Debug.TRACE, "... " + name); 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir if (isContentXML(name)) { 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir // set new values for this ZipEntry 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir ZipEntry zipEntry = new ZipEntry(name); 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir zipEntry.setMethod(ze.getMethod()); 216*cdf0e10cSrcweir zipEntry.setSize(xmlBytes.length); 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir CRC32 crc = new CRC32(); 219*cdf0e10cSrcweir crc.reset(); 220*cdf0e10cSrcweir crc.update(xmlBytes); 221*cdf0e10cSrcweir zipEntry.setCrc(crc.getValue()); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir zipEntry.setTime(System.currentTimeMillis()); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir zos.putNextEntry(zipEntry); 226*cdf0e10cSrcweir zos.write(xmlBytes); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir } else { 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir zos.putNextEntry(ze); 231*cdf0e10cSrcweir zos.write(entry.bytes); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir private boolean isContentXML(String name) { 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir String lname = name.toLowerCase(); 239*cdf0e10cSrcweir return lname.equals(XMLFILE); 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir } 242