commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [commons-compress] 04/05: COMPRESS-477 set disk number start to long
Date Sun, 15 Dec 2019 11:10:54 GMT
This is an automated email from the ASF dual-hosted git repository.

bodewig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git

commit 21025c71938d91bcaa1d55529087c0a082d22324
Author: Lee <peteralfredlee@gmail.com>
AuthorDate: Mon Dec 9 21:18:46 2019 +0800

    COMPRESS-477 set disk number start to long
    
    Basing on bodewig's rebased branch, disk number start should be a long variable.
---
 .../compress/archivers/zip/ZipArchiveEntry.java    |  6 +-
 .../archivers/zip/ZipArchiveOutputStream.java      |  2 +-
 .../commons/compress/archivers/zip/ZipFile.java    |  2 +-
 .../commons/compress/compressors/FileNameUtil.java |  1 -
 .../commons/compress/archivers/ZipTestCase.java    | 77 +++++++++++++++++++++-
 5 files changed, 79 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
index 47a3edd..ac9c5e7 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
@@ -144,7 +144,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry
     private boolean isStreamContiguous = false;
     private NameSource nameSource = NameSource.NAME;
     private CommentSource commentSource = CommentSource.COMMENT;
-    private int diskNumberStart;
+    private long diskNumberStart;
 
     /**
      * Creates a new zip entry with the specified name.
@@ -1086,7 +1086,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry
      * @return the number of the split segment this entry starts at.
      * @since 1.20
      */
-    public int getDiskNumberStart() {
+    public long getDiskNumberStart() {
         return diskNumberStart;
     }
 
@@ -1096,7 +1096,7 @@ public class ZipArchiveEntry extends java.util.zip.ZipEntry
      * @param diskNumberStart the number of the split segment this entry starts at.
      * @since 1.20
      */
-    public void setDiskNumberStart(int diskNumberStart) {
+    public void setDiskNumberStart(long diskNumberStart) {
         this.diskNumberStart = diskNumberStart;
     }
 
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
index ebd9c75..481f2f8 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
@@ -1367,7 +1367,7 @@ public class ZipArchiveOutputStream extends ArchiveOutputStream {
 
         // disk number start
         if(isSplitZip) {
-            putShort(ze.getDiskNumberStart(), buf, CFH_DISK_NUMBER_OFFSET);
+            putShort((int) ze.getDiskNumberStart(), buf, CFH_DISK_NUMBER_OFFSET);
         } else {
             System.arraycopy(ZERO, 0, buf, CFH_DISK_NUMBER_OFFSET, SHORT);
         }
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index 6f2a16c..2319456 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -862,7 +862,7 @@ public class ZipFile implements Closeable {
             }
 
             if (hasDiskStart) {
-                ze.setDiskNumberStart(z64.getDiskStartNumber().getIntValue());
+                ze.setDiskNumberStart(z64.getDiskStartNumber().getValue());
             }
         }
     }
diff --git a/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java b/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java
index af50dfc..ed94885 100644
--- a/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java
+++ b/src/main/java/org/apache/commons/compress/compressors/FileNameUtil.java
@@ -18,7 +18,6 @@
  */
 package org.apache.commons.compress.compressors;
 
-import java.io.File;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Locale;
diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
index 438c527..ba5e114 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
@@ -27,6 +27,9 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
@@ -44,6 +47,7 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
 import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.commons.compress.archivers.zip.ZipMethod;
+import org.apache.commons.compress.archivers.zip.ZipSplitReadOnlySeekableByteChannel;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.commons.compress.utils.InputStreamStatistics;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
@@ -647,16 +651,59 @@ public final class ZipTestCase extends AbstractTestCase {
         testInputStreamStatistics("COMPRESS-380/COMPRESS-380.zip", expected);
     }
 
-    @Test
-    public void buildSplitZipTest() throws IOException {
+    @Test(expected = IllegalArgumentException.class)
+    public void buildSplitZipWithTooSmallSizeThrowsException() throws IOException {
+        new ZipArchiveOutputStream(File.createTempFile("temp", "zip"), 64 * 1024 - 1);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void buildSplitZipWithTooLargeSizeThrowsException() throws IOException {
+        new ZipArchiveOutputStream(File.createTempFile("temp", "zip"), 4294967295L + 1);
+    }
+
+    @Test(expected = IOException.class)
+    public void buildSplitZipWithSegmentAlreadyExistThrowsException() throws IOException
{
         File directoryToZip = getFilesToZip();
         File outputZipFile = new File(dir, "splitZip.zip");
         long splitSize = 100 * 1024L; /* 100 KB */
         final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile,
splitSize);
 
+        // create a file that has the same name of one of the created split segments
+        File sameNameFile = new File(dir, "splitZip.z01");
+        sameNameFile.createNewFile();
+
         addFilesToZip(zipArchiveOutputStream, directoryToZip);
         zipArchiveOutputStream.close();
-        // TODO: validate the created zip files when extracting split zip is merged into
master
+    }
+
+    @Test
+    public void buildSplitZipTest() throws IOException {
+        File directoryToZip = getFilesToZip();
+        createTestSplitZipSegments();
+
+        File lastFile = new File(dir, "splitZip.zip");
+        SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
+        InputStream inputStream = Channels.newInputStream(channel);
+        ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.toString(),
true, false, true);
+
+        ArchiveEntry entry;
+        File fileToCompare;
+        InputStream inputStreamToCompare;
+        int filesNum = countNonDirectories(directoryToZip);
+        int filesCount = 0;
+        while((entry = splitInputStream.getNextEntry()) != null) {
+            if(entry.isDirectory()) {
+                continue;
+            }
+            // compare all files one by one
+            fileToCompare = new File(entry.getName());
+            inputStreamToCompare = new FileInputStream(fileToCompare);
+            Assert.assertTrue(shaded.org.apache.commons.io.IOUtils.contentEquals(splitInputStream,
inputStreamToCompare));
+            inputStreamToCompare.close();
+            filesCount++;
+        }
+        // and the number of files should equal
+        assertEquals(filesCount, filesNum);
     }
 
     private void testInputStreamStatistics(String fileName, Map<String, List<Long>>
expectedStatistics)
@@ -751,6 +798,17 @@ public final class ZipTestCase extends AbstractTestCase {
         return dir.listFiles()[0];
     }
 
+    private ZipArchiveOutputStream createTestSplitZipSegments() throws IOException {
+        File directoryToZip = getFilesToZip();
+        File outputZipFile = new File(dir, "splitZip.zip");
+        long splitSize = 100 * 1024L; /* 100 KB */
+        final ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile,
splitSize);
+
+        addFilesToZip(zipArchiveOutputStream, directoryToZip);
+        zipArchiveOutputStream.close();
+        return zipArchiveOutputStream;
+    }
+
     private void addFilesToZip(ZipArchiveOutputStream zipArchiveOutputStream, File fileToAdd)
throws IOException {
         if(fileToAdd.isDirectory()) {
             for(File file : fileToAdd.listFiles()) {
@@ -765,4 +823,17 @@ public final class ZipTestCase extends AbstractTestCase {
             zipArchiveOutputStream.closeArchiveEntry();
         }
     }
+
+    private int countNonDirectories(File file) {
+        if(!file.isDirectory()) {
+            return 1;
+        }
+
+        int result = 0;
+        for (File fileInDirectory : file.listFiles()) {
+            result += countNonDirectories(fileInDirectory);
+        }
+
+        return result;
+    }
 }


Mime
View raw message