Author: krosenvold
Date: Fri Jan 23 21:03:32 2015
New Revision: 1654382
URL: http://svn.apache.org/r1654382
Log:
Added a sample in the docs. Sample is also present in code form (test scope)
with test case
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSample.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
Modified:
commons/proper/compress/trunk/src/site/xdoc/examples.xml
commons/proper/compress/trunk/src/site/xdoc/zip.xml
Modified: commons/proper/compress/trunk/src/site/xdoc/examples.xml
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/site/xdoc/examples.xml?rev=1654382&r1=1654381&r2=1654382&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/site/xdoc/examples.xml (original)
+++ commons/proper/compress/trunk/src/site/xdoc/examples.xml Fri Jan 23
21:03:32 2015
@@ -289,6 +289,35 @@ try {
content.close();
}
]]></source>
+
+ <p>Creating a zip file with multiple threads:</p>
+
+ A simple implementation to create a zip file might be like this:
+
+<source>
+public class ScatterSample {
+
+ ParallelScatterZipCreator scatterZipCreator = new
ParallelScatterZipCreator();
+ ScatterZipOutputStream dirs =
ScatterZipOutputStream.fileBased(File.createTempFile("scatter-dirs", "tmp"));
+
+ public ScatterSample() throws IOException {
+ }
+
+ public void addEntry(ZipArchiveEntry zipArchiveEntry, InputStreamSupplier
streamSupplier) throws IOException {
+ if (zipArchiveEntry.isDirectory() &&
!zipArchiveEntry.isUnixSymlink())
+
dirs.addArchiveEntry(ZipArchiveEntryRequest.createZipArchiveEntryRequest(zipArchiveEntry,
streamSupplier));
+ else
+ scatterZipCreator.addArchiveEntry( zipArchiveEntry, streamSupplier);
+ }
+
+ public void writeTo(ZipArchiveOutputStream zipArchiveOutputStream)
+ throws IOException, ExecutionException, InterruptedException {
+ dirs.writeTo(zipArchiveOutputStream);
+ dirs.close();
+ scatterZipCreator.writeTo(zipArchiveOutputStream);
+ }
+}
+</source>
</subsection>
<subsection name="jar">
Modified: commons/proper/compress/trunk/src/site/xdoc/zip.xml
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/site/xdoc/zip.xml?rev=1654382&r1=1654381&r2=1654382&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/site/xdoc/zip.xml (original)
+++ commons/proper/compress/trunk/src/site/xdoc/zip.xml Fri Jan 23 21:03:32 2015
@@ -513,6 +513,8 @@
<p>There is no guarantee of order of the entries when writing a Zip
file with <code>ParallelScatterZipCreator</code>.</p>
+
+ See the examples section for a code sample demonstrating how to make
a zip file.
</subsection>
</section>
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSample.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSample.java?rev=1654382&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSample.java
(added)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSample.java
Fri Jan 23 21:03:32 2015
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.zip;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.ExecutionException;
+
+public class ScatterSample {
+
+ ParallelScatterZipCreator scatterZipCreator = new
ParallelScatterZipCreator();
+ ScatterZipOutputStream dirs =
ScatterZipOutputStream.fileBased(File.createTempFile("scatter-dirs", "tmp"));
+
+ public ScatterSample() throws IOException {
+ }
+
+ public void addEntry(ZipArchiveEntry zipArchiveEntry, InputStreamSupplier
streamSupplier) throws IOException {
+ if (zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink())
+
dirs.addArchiveEntry(ZipArchiveEntryRequest.createZipArchiveEntryRequest(zipArchiveEntry,
streamSupplier));
+ else
+ scatterZipCreator.addArchiveEntry( zipArchiveEntry,
streamSupplier);
+ }
+
+ public void writeTo(ZipArchiveOutputStream zipArchiveOutputStream)
+ throws IOException, ExecutionException, InterruptedException {
+ dirs.writeTo(zipArchiveOutputStream);
+ dirs.close();
+ scatterZipCreator.writeTo(zipArchiveOutputStream);
+ }
+}
Added:
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java?rev=1654382&view=auto
==============================================================================
---
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
(added)
+++
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ScatterSampleTest.java
Fri Jan 23 21:03:32 2015
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.zip;
+
+import org.apache.commons.compress.utils.IOUtils;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.ExecutionException;
+import java.util.zip.ZipEntry;
+
+import static org.junit.Assert.*;
+
+public class ScatterSampleTest {
+
+ @Test
+ public void testSample() throws Exception {
+ File result = File.createTempFile("testSample", "fe");
+
+ createFile(result);
+ checkFile(result);
+ }
+
+ private void createFile(File result) throws IOException,
ExecutionException, InterruptedException {
+ ScatterSample scatterSample = new ScatterSample();
+ ZipArchiveEntry archiveEntry = new ZipArchiveEntry("test1.xml");
+ archiveEntry.setMethod(ZipEntry.DEFLATED);
+ InputStreamSupplier supp = new InputStreamSupplier() {
+ public InputStream get() {
+ return new ByteArrayInputStream("Hello".getBytes());
+ }
+ };
+
+ scatterSample.addEntry(archiveEntry, supp);
+ ZipArchiveOutputStream zipArchiveOutputStream = new
ZipArchiveOutputStream(result);
+ scatterSample.writeTo(zipArchiveOutputStream);
+ zipArchiveOutputStream.close();
+ }
+
+ private void checkFile(File result) throws IOException {
+ ZipFile zf = new ZipFile(result);
+ ZipArchiveEntry archiveEntry1 = zf.getEntries().nextElement();
+ assertEquals( "test1.xml", archiveEntry1.getName());
+ InputStream inputStream = zf.getInputStream(archiveEntry1);
+ byte[] b = new byte[6];
+ int i = IOUtils.readFully(inputStream, b);
+ assertEquals(5, i);
+ assertEquals('H', b[0]);
+ assertEquals('o', b[4]);
+ zf.close();
+ result.delete();
+ }
+}
\ No newline at end of file