001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.template.processors.xdocreport;
020
021import java.io.IOException;
022import java.util.zip.ZipEntry;
023import java.util.zip.ZipInputStream;
024
025import org.nuxeo.ecm.core.api.Blob;
026
027/**
028 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
029 */
030public class ZipXmlHelper {
031
032    protected static final int BUFFER_SIZE = 1024 * 64; // 64K
033
034    public static final String OOO_MAIN_FILE = "content.xml";
035
036    public static final String DOCX_MAIN_FILE = "word/document.xml";
037
038    public static String readXMLContent(Blob blob, String filename) throws IOException {
039        ZipInputStream zIn = new ZipInputStream(blob.getStream());
040        ZipEntry zipEntry = zIn.getNextEntry();
041        String xmlContent = null;
042        while (zipEntry != null) {
043            if (zipEntry.getName().equals(filename)) {
044                StringBuilder sb = new StringBuilder();
045                byte[] buffer = new byte[BUFFER_SIZE];
046                int read;
047                while ((read = zIn.read(buffer)) != -1) {
048                    sb.append(new String(buffer, 0, read));
049                }
050                xmlContent = sb.toString();
051                break;
052            }
053            zipEntry = zIn.getNextEntry();
054        }
055        zIn.close();
056        return xmlContent;
057    }
058
059}