Skip to content

Commit

Permalink
include ZipStreamReader in README
Browse files Browse the repository at this point in the history
  • Loading branch information
rzymek committed Apr 26, 2020
1 parent 5b6fd89 commit 7ddb774
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
![build status](https://travis-ci.org/rzymek/opczip.svg?branch=master)
[![Maven Central](https://img.shields.io/maven-central/v/com.github.rzymek/opczip.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.rzymek%22%20AND%20a:%22opczip%22)

Drop in replacement for `java.util.ZipOutputStream`. Only Java ZIP64 implementation compatible with MS Excel.
Provides
1. `OpcZipOutputStream` - The only Java ZIP64 implementation compatible with MS Excel.
2. `ZipStreamReader` - ZIP stream reader with efficient skipping of entries.

# 1. OpcZipOutputStream

Drop in replacement for `java.util.ZipOutputStream`.

### Usage
```xml
<dependency>
<groupId>com.github.rzymek</groupId>
<artifactId>opczip</artifactId>
<version>1.0.2</version>
<version>1.2.0</version>
</dependency>
```

Expand Down Expand Up @@ -379,3 +385,35 @@ void writeEND(int entriesCount, int offset, int length) throws IOException {

It seems critical to Excel that the zip specification version is 4.5 in Local File Header if ZIP64 is used anywhere
with this zip entry (Central directory file header or Data descriptor).

# ZipStreamReader

ZIP stream reader with efficient skipping of entries.

Usage:

try (ZipStreamReader reader = new ZipStreamReader(in)) {
byte[] sharedZipped = null;
for (; ; ) {
ZipEntry entry = reader.nextEntry();
if (entry == null) {
break;
}
if ("xl/sharedStrings.xml".equals(entry.getName())) {
sharedZipped = reader.getCompressedStream() // save compressed entry without decompressing
.readAllBytes();
} else if ("xl/worksheets/sheet1.xml".equals(entry.getName())) {
byte[] sheet1 = reader.getUncompressedStream() // decompress current entry
.readAllBytes();
if (sharedZipped != null) {
byte[] shared = ZipStreamReader
// decompress previously saved entry
.uncompressed(new ByteArrayInputStream(sharedZipped))
.readAllBytes();
}
} else {
reader.skipStream(); // skip entry without decompressing
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,35 @@ void testXlsx() throws IOException {
}
}

@Test
void demo() throws IOException {
InputStream in = getClass().getResourceAsStream("/libre.xlsx");
try (ZipStreamReader reader = new ZipStreamReader(in)) {
byte[] sharedZipped = null;
for (; ; ) {
ZipEntry entry = reader.nextEntry();
if (entry == null) {
break;
}
if ("xl/sharedStrings.xml".equals(entry.getName())) {
sharedZipped = reader.getCompressedStream() // save compressed entry without decompressing
.readAllBytes();
} else if ("xl/worksheets/sheet1.xml".equals(entry.getName())) {
byte[] sheet1 = reader.getUncompressedStream() // decompress current entry
.readAllBytes();
if (sharedZipped != null) {
byte[] shared = ZipStreamReader
// decompress previously saved entry
.uncompressed(new ByteArrayInputStream(sharedZipped))
.readAllBytes();
}
} else {
reader.skipStream(); // skip entry without decompressing
}
}
}
}

@Test
void testXlsxReZipped() throws IOException {
try (ZipStreamReader reader = new ZipStreamReader(getClass().getResourceAsStream("/libre-rezipped.xlsx"))) {
Expand Down Expand Up @@ -95,12 +124,12 @@ private void test(File file) throws IOException {
}
}

static String entryToString(ZipStreamReader reader) throws IOException {
InflaterInputStream inputStream = reader.getUncompressedStream();
return toString(inputStream);
}
static String entryToString(ZipStreamReader reader) throws IOException {
InflaterInputStream inputStream = reader.getUncompressedStream();
return toString(inputStream);
}

static String toString(InputStream inputStream) throws IOException {
static String toString(InputStream inputStream) throws IOException {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}

Expand Down

0 comments on commit 7ddb774

Please sign in to comment.