File: ZipFileDuplicateEntryTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (581 lines) | stat: -rw-r--r-- 24,320 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import static org.testng.Assert.*;

/**
 * @test
 * @bug 8276123
 * @summary ZipFile::getEntry will not return a file entry when there is a
 * directory entry of the same name within a Zip File
 * @run testng/othervm ZipFileDuplicateEntryTest
 */
public class ZipFileDuplicateEntryTest {

    /**
     * Name to use for creating Zip entries
     */
    private static final String ENTRY_NAME = "entry";

    /**
     * Zip and Jar files to be created
     */
    private static final Path ZIP_FILE = Paths.get("fileDirEntry.zip");
    private static final Path ZIP_FILE2 = Paths.get("OnlyDirEntry.zip");
    private static final Path DUPLICATE_FILE_ENTRY_FILE = Paths.get("DupFIleEntry.zip");
    private static final Path TEST_JAR = Paths.get("fileDirEntry.jar");

    /**
     * Directory entry added to the Zip File.
     */
    private static final Entry DIR_ENTRY =
            Entry.of(ENTRY_NAME + "/", ZipEntry.DEFLATED,
                    "I am a Directory");

    /**
     * File entry added to the Zip File.
     */
    private static final Entry FILE_ENTRY =
            Entry.of(ENTRY_NAME, ZipEntry.DEFLATED, "I am a File");

    /**
     * Duplicate File entry added to the Zip file. This is the 2nd entry added
     * to the Zip file and is expected to be returned.
     */
    private static final Entry DUPLICATE_FILE_ENTRY =
            Entry.of(ENTRY_NAME, ZipEntry.DEFLATED, "Yet another File");
    /**
     * Entries expected to be returned via ZipFile::stream
     */
    private static final List<String> EXPECTED_ENTRIES =
            Arrays.asList(FILE_ENTRY.name, DIR_ENTRY.name);

    /**
     * Max buffer size for readAllBytes method which can be used when
     * InputStream::readAllBytes is not available
     */
    private static final int MAX_BUFFER_SIZE = 1024;

    /**
     * Flag to enable test output
     */
    private static final boolean DEBUG = false;

    /**
     * Array representing a Jar File with the entries:
     * Name: entry, contents: "I am a File"
     * Name: entry, contents: "Yet another File"
     * See createByteArray()
     */
    private static final byte[] DUPLICATE_ENTRY_JAR_BYTES = {
            (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x8,
            (byte) 0x8, (byte) 0x0, (byte) 0x60, (byte) 0x59, (byte) 0x55, (byte) 0x53, (byte) 0x8e, (byte) 0x39,
            (byte) 0x14, (byte) 0x49, (byte) 0xd, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xb, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x5, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x65, (byte) 0x6e,
            (byte) 0x74, (byte) 0x72, (byte) 0x79, (byte) 0x1, (byte) 0x0, (byte) 0x10, (byte) 0x0, (byte) 0xb,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xd,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xf3,
            (byte) 0x54, (byte) 0x48, (byte) 0xcc, (byte) 0x55, (byte) 0x48, (byte) 0x54, (byte) 0x70, (byte) 0xcb,
            (byte) 0xcc, (byte) 0x49, (byte) 0x5, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4,
            (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x0, (byte) 0x60, (byte) 0x59,
            (byte) 0x55, (byte) 0x53, (byte) 0xe1, (byte) 0x4c, (byte) 0x29, (byte) 0xa4, (byte) 0x12, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x10, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x5, (byte) 0x0,
            (byte) 0x14, (byte) 0x0, (byte) 0x65, (byte) 0x6e, (byte) 0x74, (byte) 0x72, (byte) 0x79, (byte) 0x1,
            (byte) 0x0, (byte) 0x10, (byte) 0x0, (byte) 0x10, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x12, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x8b, (byte) 0x4c, (byte) 0x2d, (byte) 0x51, (byte) 0x48,
            (byte) 0xcc, (byte) 0xcb, (byte) 0x2f, (byte) 0xc9, (byte) 0x48, (byte) 0x2d, (byte) 0x52, (byte) 0x70,
            (byte) 0xcb, (byte) 0xcc, (byte) 0x49, (byte) 0x5, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
            (byte) 0x2, (byte) 0x14, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x8, (byte) 0x8,
            (byte) 0x0, (byte) 0x60, (byte) 0x59, (byte) 0x55, (byte) 0x53, (byte) 0x8e, (byte) 0x39, (byte) 0x14,
            (byte) 0x49, (byte) 0xd, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xb, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x5, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x65, (byte) 0x6e, (byte) 0x74, (byte) 0x72, (byte) 0x79,
            (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, (byte) 0x0, (byte) 0x14, (byte) 0x0,
            (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x0, (byte) 0x60, (byte) 0x59, (byte) 0x55, (byte) 0x53,
            (byte) 0xe1, (byte) 0x4c, (byte) 0x29, (byte) 0xa4, (byte) 0x12, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x10, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x5, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x44, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x65, (byte) 0x6e,
            (byte) 0x74, (byte) 0x72, (byte) 0x79, (byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2, (byte) 0x0, (byte) 0x2, (byte) 0x0, (byte) 0x66,
            (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x8d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
            (byte) 0x0,
    };

    /**
     * Create Zip files used by the tests.
     *
     * @throws IOException If an error occurs
     */
    @BeforeTest
    public static void setup() throws IOException {

        /**
         *  Zip contains two entries named "entry" and "entry/"
         */
        Files.deleteIfExists(ZIP_FILE);
        try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(ZIP_FILE))) {
            zos.putNextEntry(new ZipEntry(FILE_ENTRY.name));
            zos.write(FILE_ENTRY.bytes);
            zos.closeEntry();
            zos.putNextEntry(new ZipEntry(DIR_ENTRY.name));
            zos.write(DIR_ENTRY.bytes);
            zos.closeEntry();
        }

        /**
         *  Jar contains two entries named "entry" and "entry/"
         */
        Files.deleteIfExists(TEST_JAR);
        try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(TEST_JAR))) {
            jos.putNextEntry(new JarEntry(FILE_ENTRY.name));
            jos.write(FILE_ENTRY.bytes);
            jos.closeEntry();
            jos.putNextEntry(new JarEntry(DIR_ENTRY.name));
            jos.write(DIR_ENTRY.bytes);
            jos.closeEntry();
        }

        /**
         *  Zip contains the entry "entry/"
         */
        Files.deleteIfExists(ZIP_FILE2);
        try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(ZIP_FILE2))) {
            zos.putNextEntry(new ZipEntry(DIR_ENTRY.name));
            zos.write(DIR_ENTRY.bytes);
            zos.closeEntry();
        }

        /**
         *  Create a Jar that contains two entries named "entry"
         */
        Files.deleteIfExists(DUPLICATE_FILE_ENTRY_FILE);
        Files.write(DUPLICATE_FILE_ENTRY_FILE, DUPLICATE_ENTRY_JAR_BYTES);
    }

    /**
     * Clean up after the test run
     *
     * @throws IOException If an error occurs
     */
    @AfterTest
    public static void cleanup() throws IOException {
        Files.deleteIfExists(ZIP_FILE);
        Files.deleteIfExists(ZIP_FILE2);
        Files.deleteIfExists(DUPLICATE_FILE_ENTRY_FILE);
        Files.deleteIfExists(TEST_JAR);
    }

    /**
     * DataProvider used to specify the Zip entries to use
     *
     * @return The Entry to use within the test
     */
    @DataProvider
    public Object[][] entries() {
        return new Object[][]{
                {FILE_ENTRY},
                {DIR_ENTRY}
        };
    }

    /**
     * Test whether ZipFile::getEntry can find a directory entry within a Zip
     * file specifying "name" vs "name/"
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void readDirWithoutSlash() throws IOException {
        System.out.printf("%n%n**** readDirWithoutSlash ***%n");
        try (ZipFile zip = new ZipFile(ZIP_FILE2.toString())) {
            ZipEntry ze = zip.getEntry(ENTRY_NAME);
            if (DEBUG) {
                System.out.printf("    Entry:%s, found:%s%n", ENTRY_NAME, ze != null);
            }
            assertNotNull(ze);
            assertTrue(ze.isDirectory());
            try (InputStream in = zip.getInputStream(ze)) {
                byte[] bytes = in.readAllBytes();
                if (DEBUG) {
                    System.out.printf("name: %s, isDirectory: %s, payload= %s%n",
                            ze.getName(), ze.isDirectory(), new String(bytes));
                }
                assertEquals(bytes, DIR_ENTRY.bytes,
                        String.format("Expected payload: %s",
                                new String(DIR_ENTRY.bytes)));
            }
        }
    }

    /**
     * Validate that ZipFile::getEntry will return the correct entry when a file
     * and directory have the same name
     *
     * @param entry The entry to search for
     * @throws IOException If an error occurs
     */
    @Test(dataProvider = "entries")
    public void testSameFileDirEntryName(Entry entry) throws IOException {
        System.out.printf("%n%n**** testSameFileDirEntryName ***%n");

        try (ZipFile zip = new ZipFile(ZIP_FILE.toString())) {
            ZipEntry ze = zip.getEntry(entry.name);
            if (DEBUG) {
                System.out.printf("    Entry:%s, found:%s%n", entry.name, ze != null);
            }
            assertNotNull(ze);
            try (InputStream in = zip.getInputStream(ze)) {
                byte[] bytes = in.readAllBytes();
                if (DEBUG) {
                    System.out.printf("name: %s, isDirectory: %s, payload= %s%n",
                            ze.getName(), ze.isDirectory(), new String(bytes));
                }
                assertEquals(entry.bytes, bytes,
                        String.format("Expected payload: %s", new String(entry.bytes)));
            }
        }
    }

    /**
     * Validate that ZipFile::getEntry will return the correct entry, which
     * is the second entry, when there are duplicate entries within the Zip file.
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void DupFileEntryTest() throws IOException {
        System.out.printf("%n%n**** DupFileEntryTest ***%n");
        try (ZipFile zip =
                     new ZipFile(DUPLICATE_FILE_ENTRY_FILE.toString())) {
            ZipEntry ze = zip.getEntry(ENTRY_NAME);
            if (DEBUG) {
                System.out.printf("    Entry:%s, found:%s%n", ENTRY_NAME, ze != null);
            }
            assertNotNull(ze);
            try (InputStream in = zip.getInputStream(ze)) {
                byte[] bytes = in.readAllBytes();
                if (DEBUG) {
                    System.out.printf("name: %s, isDirectory: %s, payload= %s%n",
                            ze.getName(), ze.isDirectory(), new String(bytes));
                }
                assertEquals(bytes, DUPLICATE_FILE_ENTRY.bytes,
                        String.format("Expected payload: %s", new String(DUPLICATE_FILE_ENTRY.bytes)));
            }
        }
    }

    /**
     * Verify that ZipInputStream can be used to read all Zip entries including
     * a file and directory entry with the same name
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void ZipInputStreamTest() throws IOException {
        System.out.printf("%n%n**** ZipInputStreamTest ***%n");
        try (ZipInputStream zis = new ZipInputStream(
                new FileInputStream(ZIP_FILE.toFile()))) {
            ZipEntry zipEntry = zis.getNextEntry();
            assertNotNull(zipEntry);
            while (zipEntry != null) {
                Entry e;
                if (zipEntry.getName().equals(FILE_ENTRY.name)) {
                    e = FILE_ENTRY;
                } else if (zipEntry.getName().equals(DIR_ENTRY.name)) {
                    e = DIR_ENTRY;
                } else {
                    throw new RuntimeException(
                            String.format("Invalid Zip entry: %s", zipEntry.getName()));
                }
                assertEquals(zipEntry.getMethod(), e.method);
                assertEquals(zis.readAllBytes(), e.bytes,
                        String.format("Expected payload: %s", new String(e.bytes)));
                zipEntry = zis.getNextEntry();
            }
        }
    }

    /**
     * Verify that ZipFile::stream returns all Zip entries including
     * a file and directory entry with the same name
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void ZipFileStreamTest() throws IOException {
        System.out.printf("%n%n**** ZipFileStreamTest ***%n");
        try (ZipFile zf = new ZipFile(ZIP_FILE.toFile())) {
            List<? extends ZipEntry> entries = zf.stream().collect(Collectors.toList());
            assertEquals(EXPECTED_ENTRIES.size(), entries.size());
            for (ZipEntry e : entries) {
                assertTrue(EXPECTED_ENTRIES.contains(e.getName()));
            }
        }
    }

    /**
     * Verify that JarFile can be used to read all the entries including
     * a file and directory entry with the same name
     *
     * @param entry The entry to validate
     * @throws IOException If an error occurs
     */
    @Test(dataProvider = "entries")
    public static void JarFileInputStreamTest(Entry entry) throws IOException {
        System.out.printf("%n%n**** JarFileInputStreamTest ***%n");
        try (JarFile jarFile = new JarFile(TEST_JAR.toFile())) {
            JarEntry je = jarFile.getJarEntry(entry.name);
            assertNotNull(je);
            if (DEBUG) {
                System.out.printf("Entry Name: %s, method: %s, Expected Method: %s%n",
                        entry.name, je.getMethod(), entry.method);
            }
            assertEquals(entry.method, je.getMethod());
            try (InputStream in = jarFile.getInputStream(je)) {
                byte[] bytes = in.readAllBytes();
                if (DEBUG) {
                    System.out.printf("bytes= %s, expected=%s%n",
                            new String(bytes), new String(entry.bytes));
                }
                assertEquals(bytes, entry.bytes,
                        String.format("Expected payload: %s", new String(entry.bytes)));
            }
        }
    }

    /**
     * Verify that JarInputStream can be used to read all entries including
     * a file and directory entry with the same name
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void JarInputStreamTest() throws IOException {
        System.out.printf("%n%n**** JarInputStreamTest ***%n");
        try (JarInputStream jis = new JarInputStream(
                new FileInputStream(TEST_JAR.toFile()))) {
            JarEntry jarEntry = jis.getNextJarEntry();
            assertNotNull(jarEntry);
            while (jarEntry != null) {
                Entry e;
                if (jarEntry.getName().equals(FILE_ENTRY.name)) {
                    e = FILE_ENTRY;
                } else if (jarEntry.getName().equals(DIR_ENTRY.name)) {
                    e = DIR_ENTRY;
                } else {
                    throw new RuntimeException(
                            String.format("Invalid Jar entry: %s", jarEntry.getName()));
                }
                assertEquals(jarEntry.getMethod(), e.method);
                assertEquals(jis.readAllBytes(), e.bytes,
                        String.format("Expected payload: %s", new String(e.bytes)));
                jarEntry = jis.getNextJarEntry();
            }
        }
    }

    /**
     * Verify that JarURLConnection can be used to access all the entries including
     * a file and directory entry with the same name within a jar file
     *
     * @param entry The entry to validate
     * @throws IOException If an error occurs
     */
    @Test(dataProvider = "entries")
    public void JarURLConnectionTest(Entry entry) throws Exception {
        System.out.printf("%n%n**** JarURLConnectionTest ***%n");
        URL url = new URL("jar:" + TEST_JAR.toUri().toURL() + "!/" + entry.name);
        if (DEBUG) {
            System.out.printf("URL=%s%n", url);
        }
        JarURLConnection con = (JarURLConnection) url.openConnection();
        con.connect();
        JarEntry je = con.getJarEntry();
        try (JarFile jarFile = con.getJarFile()) {
            assertNotNull(je);
            assertNotNull(jarFile);
            assertNull(con.getAttributes());
            assertNull(con.getMainAttributes());
            assertNull(con.getManifest());
            assertEquals(je.getName(), entry.name);
            assertEquals(con.getEntryName(), entry.name);
            assertEquals(je.getMethod(), entry.method);
            assertEquals(con.getJarFileURL(), TEST_JAR.toUri().toURL());
            if (DEBUG) {
                System.out.printf("   getEntryName: %s,  getJarFileURL:%s%n",
                        con.getEntryName(), con.getJarFileURL());
                System.out.printf("   Jar Entry= %s, size= %s%n", je.getName(), je.getSize());
            }

            try (InputStream is = jarFile.getInputStream(je)) {
                byte[] bytes = is.readAllBytes();
                if (DEBUG) {
                    System.out.printf("   Bytes read:%s%n", new String(bytes));
                }
                assertEquals(bytes, entry.bytes,
                        String.format("Expected payload: %s", new String(entry.bytes)));
            }
        }
    }

    /**
     * Verify that JarFile::stream returns all entries including
     * a file and directory entry with the same name
     *
     * @throws IOException If an error occurs
     */
    @Test
    public void JarFileStreamTest() throws IOException {
        System.out.printf("%n%n**** JarFileStreamTest ***%n");
        try (JarFile jf = new JarFile(TEST_JAR.toFile())) {
            List<? extends JarEntry> entries = jf.stream().collect(Collectors.toList());
            assertEquals(EXPECTED_ENTRIES.size(), jf.size());
            for (JarEntry e : entries) {
                assertTrue(EXPECTED_ENTRIES.contains(e.getName()));
            }
        }
    }

    /**
     * Method used to read  the bytes from an InputStream.  This method is
     * here so that the test could be backported to JDK 8 if needed as
     * InputStream::readAllBytes() does not exist
     *
     * @param is The InputStream to read from
     * @return The byte array representing bytes read from the InputStream
     * @throws IOException If an error occurs
     */
    public static byte[] readAllBytes(InputStream is) throws IOException {
        byte[] data = new byte[MAX_BUFFER_SIZE];
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int len;
        while ((len = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, len);
        }
        buffer.flush();
        return buffer.toByteArray();
    }

    /**
     * Method used to create a byte[] representing a Jar file with
     * duplicate file entries.  This uses ZipArchiveOutputStream as ZipOutputStream
     * will fail with a "java.util.zip.ZipException: duplicate entry".
     */
//    public static void  createJarWithDuplicateFileEntries() throws IOException {
//    Files.deleteIfExists(DUPFILE_ENTRY_FILE);
//    try (ZipArchiveOutputStream zos =
//                     new ZipArchiveOutputStream(DUPFILE_ENTRY_FILE.toFile())) {
//            zos.putArchiveEntry(new ZipArchiveEntry(FILE_ENTRY.name));
//            zos.write(FILE_ENTRY.bytes);
//            zos.putArchiveEntry(new ZipArchiveEntry(FILE_ENTRY.name));
//            zos.write("Yet another File".getBytes(StandardCharsets.UTF_8));
//            zos.closeArchiveEntry();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        byte[] jarBytes = Files.readAllBytes(DUPFILE_ENTRY_FILE);
//        String result = createByteArray(jarBytes, "DUPLICATE_ENTRY_JAR_BYTES");
//        System.out.println(result);
//    }

    /**
     * Utility method which takes a byte array and converts to byte array
     * declaration.  For example:
     * <pre>
     *     {@code
     *        var fooJar = Files.readAllBytes(Path.of("foo.jar"));
     *        var result = createByteArray(fooJar, "FOOBYTES");
     *      }
     * </pre>
     *
     * @param bytes A byte array used to create a byte array declaration
     * @param name  Name to be used in the byte array declaration
     * @return The formatted byte array declaration
     */
    public static String createByteArray(byte[] bytes, String name) {
        StringBuilder sb = new StringBuilder(bytes.length * 5);
        Formatter fmt = new Formatter(sb);
        fmt.format("    public static byte[] %s = {", name);
        final int linelen = 8;
        for (int i = 0; i < bytes.length; i++) {
            if (i % linelen == 0) {
                fmt.format("%n        ");
            }
            fmt.format(" (byte) 0x%x,", bytes[i] & 0xff);
        }
        fmt.format("%n    };%n");
        return sb.toString();
    }

    /**
     * Represents an entry in a Zip file. An entry encapsulates a name, a
     * compression method, and its contents/data.
     */
    public static class Entry {
        public final String name;
        public final int method;
        public final byte[] bytes;

        public Entry(String name, int method, String contents) {
            this.name = name;
            this.method = method;
            this.bytes = contents.getBytes(StandardCharsets.UTF_8);
        }

        public static Entry of(String name, int method, String contents) {
            return new Entry(name, method, contents);
        }
    }
}