1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import org.testng.annotations.AfterMethod;
  24 import org.testng.annotations.BeforeMethod;
  25 import org.testng.annotations.Test;
  26 
  27 import java.io.*;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.List;
  31 import java.util.zip.ZipEntry;
  32 import java.util.zip.ZipFile;
  33 import java.util.zip.ZipOutputStream;
  34 
  35 import static org.testng.Assert.assertTrue;
  36 
  37 /**
  38  * @test
  39  * @bug 8226530
  40  * @summary ZIP File System tests that leverage DirectoryStream
  41  * @modules java.base
  42  * @compile Zip64SizeTest.java
  43  * @run testng Zip64SizeTest
  44  */
  45 public class Zip64SizeTest {
  46 
  47     private static final int BUFFER_SIZE = 2048;
  48     // ZIP file to create
  49     private static final String ZIP_FILE_NAME = "Zip64SizeTest.zip";
  50     // File that will be created with a size greater than 0xFFFFFFFF
  51     private static final String LARGE_FILE_NAME = "LargeZipEntry.txt";
  52     // File that will be created with a size less than 0xFFFFFFFF
  53     private static final String SMALL_FILE_NAME = "SmallZipEntry.txt";
  54     // List of files to be added to the ZIP file
  55     private static final List<String> ZIP_ENTRIES = List.of(LARGE_FILE_NAME,
  56             SMALL_FILE_NAME);
  57     private static final long LARGE_FILE_SIZE = 5L * 1024L * 1024L * 1024L; // 5GB
  58     private static final long SMALL_FILE_SIZE = 0x100000L; // 1024L x 1024L;
  59 
  60     /**
  61      * Validate that if the size of a ZIP entry exceeds 0xFFFFFFFF, that the
  62      * correct size is returned from the ZIP64 Extended information.
  63      * @throws IOException
  64      */
  65     @Test
  66     private static void validateZipEntrySizes() throws IOException {
  67         createFiles();
  68         createZipFile();
  69         System.out.println("Validating Zip Entry Sizes");
  70         try (ZipFile zip = new ZipFile(ZIP_FILE_NAME)) {
  71             ZipEntry ze = zip.getEntry(LARGE_FILE_NAME);
  72             System.out.printf("Entry: %s, size= %s%n", ze.getName(), ze.getSize());
  73             assertTrue(ze.getSize() == LARGE_FILE_SIZE);
  74             ze = zip.getEntry(SMALL_FILE_NAME);
  75             System.out.printf("Entry: %s, size= %s%n", ze.getName(), ze.getSize());
  76             assertTrue(ze.getSize() == SMALL_FILE_SIZE);
  77 
  78         }
  79     }
  80 
  81     /**
  82      * Delete the files created for use by the test
  83      * @throws IOException if an error occurs deleting the files
  84      */
  85     private static void deleteFiles() throws IOException {
  86         Files.deleteIfExists(Path.of(ZIP_FILE_NAME));
  87         Files.deleteIfExists(Path.of(LARGE_FILE_NAME));
  88         Files.deleteIfExists(Path.of(SMALL_FILE_NAME));
  89     }
  90 
  91     /**
  92      * Create the ZIP file adding an entry whose size exceeds 0xFFFFFFFF
  93      * @throws IOException if an error occurs creating the ZIP File
  94      */
  95     private static void createZipFile() throws IOException {
  96         try (FileOutputStream fos = new FileOutputStream(ZIP_FILE_NAME);
  97              ZipOutputStream zos = new ZipOutputStream(fos)) {
  98             System.out.printf("Creating Zip file: %s%n", ZIP_FILE_NAME);
  99             for (String srcFile : ZIP_ENTRIES) {
 100                 System.out.printf("...Adding Entry: %s%n", srcFile);
 101                 File fileToZip = new File(srcFile);
 102                 try (FileInputStream fis = new FileInputStream(fileToZip)) {
 103                     ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
 104                     zipEntry.setSize(fileToZip.length());
 105                     zos.putNextEntry(zipEntry);
 106                     byte[] bytes = new byte[BUFFER_SIZE];
 107                     int length;
 108                     while ((length = fis.read(bytes)) >= 0) {
 109                         zos.write(bytes, 0, length);
 110                     }
 111                 }
 112             }
 113         }
 114     }
 115 
 116     /**
 117      * Create the files that will be added to the ZIP file
 118      * @throws IOException if there is a problem  creating the files
 119      */
 120     private static void createFiles() throws IOException {
 121         try (RandomAccessFile largeFile = new RandomAccessFile(LARGE_FILE_NAME, "rw");
 122              RandomAccessFile smallFile = new RandomAccessFile(SMALL_FILE_NAME, "rw")) {
 123             System.out.printf("Creating %s%n", LARGE_FILE_NAME);
 124             largeFile.setLength(LARGE_FILE_SIZE);
 125             System.out.printf("Creating %s%n", SMALL_FILE_NAME);
 126             smallFile.setLength(SMALL_FILE_SIZE);
 127         }
 128     }
 129 
 130     /**
 131      * Make sure the needed test files do not exist prior to executing the test
 132      * @throws IOException
 133      */
 134     @BeforeMethod
 135     public void setUp() throws IOException {
 136         deleteFiles();
 137     }
 138 
 139     /**
 140      * Remove the files created for the test
 141      * @throws IOException
 142      */
 143     @AfterMethod
 144     public void tearDown() throws IOException {
 145         deleteFiles();
 146     }
 147 }