1 package org.codehaus.plexus.archiver.zip; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.UncheckedIOException; 7 import java.nio.file.Files; 8 9 import org.apache.commons.compress.archivers.zip.UnixStat; 10 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; 11 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; 12 import org.apache.commons.compress.utils.IOUtils; 13 import org.codehaus.plexus.util.DirectoryScanner; 14 import org.junit.jupiter.api.Disabled; 15 import org.junit.jupiter.api.Test; 16 17 @SuppressWarnings("ResultOfMethodCallIgnored") 18 @Disabled 19 class ConcurrentJarCreatorTest { 20 21 @Test 22 void concurrent() throws Exception { 23 File home = new File(System.getProperty("user.home")); 24 File result = new File(home, "multiStream2-parallel.zip"); 25 ConcurrentJarCreator zipCreator = 26 new ConcurrentJarCreator(Runtime.getRuntime().availableProcessors()); 27 28 final File file1 = new File(home, "lsrc/plexus"); 29 doAddAll(file1.getPath(), zipCreator); 30 31 ZipArchiveOutputStream zos = createZipARchiveOutputStream(result); 32 zipCreator.writeTo(zos); 33 zos.close(); 34 System.out.println("Concurrent:" + zipCreator.getStatisticsMessage()); 35 } 36 37 @Test 38 void concurrent2() throws Exception { 39 concurrent(); 40 } 41 42 @Test 43 @Disabled 44 void classic() throws Exception { 45 long startAt = System.currentTimeMillis(); 46 File home = new File(System.getProperty("user.home")); 47 File result = new File(home, "multiStream2-classic.zip"); 48 49 final File file1 = new File(home, "lsrc/plexus"); 50 ZipArchiveOutputStream zos = createZipARchiveOutputStream(result); 51 doAddAll(file1.getPath(), zos); 52 zos.close(); 53 System.out.println("linear:" + (System.currentTimeMillis() - startAt) + "ms"); 54 } 55 56 private ZipArchiveOutputStream createZipARchiveOutputStream(File result) throws IOException { 57 ZipArchiveOutputStream zos = new ZipArchiveOutputStream(result); 58 zos.setEncoding("UTF-8"); 59 return zos; 60 } 61 62 private void doAddAll(String base, ConcurrentJarCreator mos) throws IOException { 63 64 DirectoryScanner ds = getIncludedFiles(base); 65 66 for (String fileName : ds.getIncludedFiles()) { 67 final File file = new File(base, fileName); 68 ZipArchiveEntry za = createZipArchiveEntry(file, fileName); 69 70 mos.addArchiveEntry( 71 za, 72 () -> { 73 try { 74 return file.isFile() ? Files.newInputStream(file.toPath()) : null; 75 } catch (IOException e) { 76 throw new UncheckedIOException(e); 77 } 78 }, 79 true); 80 } 81 } 82 83 private DirectoryScanner getIncludedFiles(String base) { 84 DirectoryScanner ds = new DirectoryScanner(); 85 ds.setBasedir(base); 86 ds.scan(); 87 return ds; 88 } 89 90 private void doAddAll(String base, ZipArchiveOutputStream mos) throws IOException { 91 DirectoryScanner ds = getIncludedFiles(base); 92 93 for (String fileName : ds.getIncludedFiles()) { 94 final File file = new File(base, fileName); 95 ZipArchiveEntry za = createZipArchiveEntry(file, fileName); 96 97 mos.putArchiveEntry(za); 98 if (file.isFile()) { 99 try (InputStream input = Files.newInputStream(file.toPath())) { 100 IOUtils.copy(input, mos); 101 } 102 } 103 mos.closeArchiveEntry(); 104 } 105 } 106 107 @SuppressWarnings("OctalInteger") 108 private ZipArchiveEntry createZipArchiveEntry(File file, String name) { 109 ZipArchiveEntry za = new ZipArchiveEntry(file, name); 110 if (file.isDirectory()) { 111 za.setMethod(ZipArchiveEntry.STORED); 112 za.setSize(0); 113 za.setUnixMode(UnixStat.DIR_FLAG | 0664); 114 } else { 115 za.setMethod(ZipArchiveEntry.DEFLATED); 116 za.setSize(file.length()); 117 za.setUnixMode(UnixStat.FILE_FLAG | 0664); 118 } 119 za.setTime(file.lastModified()); 120 return za; 121 } 122 }