Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Huge refactor and stabilize #13

Merged
merged 8 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/org/swisspush/mirror/UnzippedResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.swisspush.mirror;

import io.vertx.core.buffer.Buffer;

/**
* A holder (tuple) for an unzipped entry: filename (as in ZIP, i.e. relative path) plus file-content as Vert.x Buffer
*/
public class UnzippedResource {
public final String filename;
public final Buffer buffer = Buffer.buffer();

public UnzippedResource(String filename) {
this.filename = filename;
}
}
114 changes: 0 additions & 114 deletions src/main/java/org/swisspush/mirror/ZipFileEntryIterator.java

This file was deleted.

79 changes: 79 additions & 0 deletions src/main/java/org/swisspush/mirror/ZipIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.swisspush.mirror;


import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;

/**
* Iterates over the content of a zip.
* The zip must be provided as InputStream
* Only files within the ZIP are considered as valid zip entries (i.e. directory entries in ZIP are skipped/ignored).
*
* @author: Oliver Henning
*/
public class ZipIterator {

private ZipInputStream zis;
private UnzippedResource unzippedResource;

// assume single threaded access
private byte[] buf = new byte[1024];

public ZipIterator(InputStream is) {
zis = new ZipInputStream(is);
}

/**
* @return the next UnzippedResource (containing relative path as in ZIP-Entry and unZIPped content as a Vert.x Buffer)
*
* @throws IOException
* @throws ZipException
* @throws NoSuchElementException
*/
public UnzippedResource next() throws IOException, NoSuchElementException {
if (unzippedResource == null) {
if (!hasNext()) {
throw new NoSuchElementException("done");
}
}
UnzippedResource current = unzippedResource;
unzippedResource = null;
return current;
}

/**
* @return true when {@link #next() would return a non-null value}
* @throws IOException
* @throws ZipException
*/
public boolean hasNext() throws IOException {
if (zis == null) {
return false;
}
while (unzippedResource == null) {
ZipEntry ze = zis.getNextEntry();
if (ze == null) {
try {
zis.close();
} catch (IOException ex) {
// silently close
}
zis = null;
return false;
}
String filename = ze.getName();
if (!filename.endsWith("/")) {
Copy link
Contributor

@ljucam ljucam Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we really have filenames in a zip stream that represents not files but folders?
And yeah, I know that for e.g. ZipOutputStream can handle empty directories by adding a forward-slash / after the folder name. But can we really have this here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was so in the old implementation - 'directories' (identified by trailing slash) where explicit skipped.
And obviously yes, 'directories' can be listed in a ZIP - for whatever reason

unzippedResource = new UnzippedResource(filename);
int count;
while ((count = zis.read(buf)) >= 0) {
unzippedResource.buffer.appendBytes(buf, 0, count);
}
}
}
return true;
}
}