19package org.openoffice.xmerge.converter.xml;
22import java.util.ListIterator;
23import java.util.LinkedList;
24import java.util.zip.ZipInputStream;
25import java.util.zip.ZipOutputStream;
26import java.util.zip.ZipEntry;
27import java.util.zip.CRC32;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.io.IOException;
31import java.io.ByteArrayOutputStream;
43 private static final String CONTENTXML =
"content.xml";
45 private static final String STYLEXML =
"styles.xml";
46 private static final String METAXML =
"meta.xml";
47 private static final String SETTINGSXML =
"settings.xml";
48 private static final String MANIFESTXML =
"META-INF/manifest.xml";
50 private static final int BUFFERSIZE = 1024;
52 private final List<Entry> entryList;
54 private int contentIndex = -1;
55 private int styleIndex = -1;
56 private int metaIndex = -1;
57 private int settingsIndex = -1;
58 private int manifestIndex = -1;
81 void read(InputStream is)
throws IOException {
83 ZipInputStream zis =
new ZipInputStream(is);
87 while ((ze = zis.getNextEntry()) !=
null) {
91 Debug.log(Debug.TRACE,
"reading entry: " + name);
93 ByteArrayOutputStream baos =
new ByteArrayOutputStream();
96 byte buffer[] =
new byte[BUFFERSIZE];
98 while ((len = zis.read(buffer)) > 0) {
99 baos.write(buffer, 0, len);
102 byte bytes[] = baos.toByteArray();
105 entryList.add(entry);
109 if (
name.equalsIgnoreCase(CONTENTXML)) {
112 else if (
name.equalsIgnoreCase(STYLEXML)) {
115 else if (
name.equalsIgnoreCase(METAXML)) {
118 else if (
name.equalsIgnoreCase(SETTINGSXML)) {
121 else if (
name.equalsIgnoreCase(MANIFESTXML)) {
137 byte[] getContentXMLBytes() {
139 return getEntryBytes(contentIndex);
150 byte[] getStyleXMLBytes() {
152 return getEntryBytes(styleIndex);
163 byte[] getMetaXMLBytes() {
164 return getEntryBytes(metaIndex);
175 byte[] getSettingsXMLBytes() {
176 return getEntryBytes(settingsIndex);
187 byte[] getManifestXMLBytes() {
188 return getEntryBytes(manifestIndex);
200 byte[] getNamedBytes(String name) {
207 for (
int i = 0;
i < entryList.size();
i++) {
208 Entry e = entryList.get(i);
210 if (e.zipEntry.getName().equals(name)) {
211 return getEntryBytes(i);
229 void setNamedBytes(String name,
byte[]
bytes) {
230 for (
int i = 0;
i < entryList.size();
i++) {
231 Entry e = entryList.get(i);
233 if (e.zipEntry.getName().equals(name)) {
234 setEntryBytes(i,
bytes, name);
241 setEntryBytes(-1,
bytes, name);
254 private byte[] getEntryBytes(
int index) {
259 Entry entry = entryList.get(index);
270 void setContentXMLBytes(
byte bytes[]) {
272 contentIndex = setEntryBytes(contentIndex,
bytes, CONTENTXML);
280 void setStyleXMLBytes(
byte bytes[]) {
282 styleIndex = setEntryBytes(styleIndex,
bytes, STYLEXML);
290 void setMetaXMLBytes(
byte bytes[]) {
292 metaIndex = setEntryBytes(metaIndex,
bytes, METAXML);
300 void setSettingsXMLBytes(
byte bytes[]) {
302 settingsIndex = setEntryBytes(settingsIndex,
bytes, SETTINGSXML);
310 void setManifestXMLBytes(
byte bytes[]) {
311 manifestIndex = setEntryBytes(manifestIndex,
bytes, MANIFESTXML);
328 private int setEntryBytes(
int index,
byte bytes[], String name) {
332 Entry entry = entryList.get(index);
333 name = entry.zipEntry.getName();
334 int method = entry.zipEntry.getMethod();
336 ZipEntry ze = createZipEntry(name,
bytes, method);
343 ZipEntry ze = createZipEntry(name,
bytes, ZipEntry.DEFLATED);
345 entryList.add(entry);
346 index = entryList.size() - 1;
359 void write(OutputStream os)
throws IOException {
361 Debug.log(Debug.TRACE,
"Writing out the following entries into zip.");
363 ZipOutputStream zos =
new ZipOutputStream(os);
365 ListIterator<Entry> iterator = entryList.listIterator();
366 while (iterator.hasNext()) {
368 Entry entry = iterator.next();
369 ZipEntry ze = entry.zipEntry;
373 Debug.log(Debug.TRACE,
"... " + name);
375 zos.putNextEntry(ze);
376 zos.write(entry.bytes);
391 private ZipEntry createZipEntry(String name,
byte bytes[],
int method) {
393 ZipEntry ze =
new ZipEntry(name);
395 ze.setMethod(method);
396 ze.setSize(
bytes.length);
398 CRC32 crc =
new CRC32();
401 ze.setCrc(crc.getValue());
403 ze.setTime(
System.currentTimeMillis());
416 ZipEntry zipEntry =
null;
420 this.zipEntry = zipEntry;
std::deque< std::string > LinkedList
This inner class is used as a data structure for holding a ZipEntry info and its corresponding bytes.
This class is used for logging debug messages.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
std::vector< sal_uInt8 > bytes