• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Code Intelligence GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.code_intelligence.jazzer.utils;
18 
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.lang.IllegalArgumentException;
29 import java.nio.file.FileVisitResult;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.nio.file.SimpleFileVisitor;
34 import java.nio.file.attribute.BasicFileAttributes;
35 import java.util.ArrayList;
36 import java.util.Enumeration;
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Set;
40 import java.util.jar.JarEntry;
41 import java.util.jar.JarFile;
42 import java.util.stream.Stream;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipInputStream;
45 import java.util.zip.ZipOutputStream;
46 
47 public final class ZipUtils {
ZipUtils()48   private ZipUtils() {}
49 
mergeZipToZip(String src, ZipOutputStream zos, Set<String> skipFiles)50   public static Set<String> mergeZipToZip(String src, ZipOutputStream zos, Set<String> skipFiles)
51       throws IOException {
52     HashSet<String> filesAdded = new HashSet<>();
53     try (JarFile jarFile = new JarFile(src)) {
54       // Copy entries from src to dst (jarFile to ZipOutputStream)
55       Enumeration<JarEntry> allEntries = jarFile.entries();
56       while (allEntries.hasMoreElements()) {
57         JarEntry entry = allEntries.nextElement();
58         if (skipFiles != null && skipFiles.contains(entry.getName())) {
59           continue;
60         }
61 
62         zos.putNextEntry(new ZipEntry(entry.getName()));
63         try (InputStream is = jarFile.getInputStream(entry)) {
64           byte[] buf = new byte[1024];
65           int i = 0;
66           while ((i = is.read(buf)) != -1) {
67             zos.write(buf, 0, i);
68           }
69 
70           zos.closeEntry();
71           filesAdded.add(entry.getName());
72         }
73       }
74     }
75 
76     return filesAdded;
77   }
78 
mergeDirectoryToZip(String src, ZipOutputStream zos, Set<String> skipFiles)79   public static Set<String> mergeDirectoryToZip(String src, ZipOutputStream zos,
80       Set<String> skipFiles) throws IllegalArgumentException, IOException {
81     HashSet<String> filesAdded = new HashSet<>();
82     File sourceDir = new File(src);
83     if (!sourceDir.isDirectory()) {
84       throw new IllegalArgumentException("Argument src must be a directory.");
85     }
86 
87     Files.walkFileTree(sourceDir.toPath(), new SimpleFileVisitor<Path>() {
88       public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
89         String zipPath = sourceDir.toPath().relativize(file).toString();
90         if (skipFiles.stream().anyMatch(zipPath::endsWith)) {
91           return FileVisitResult.CONTINUE;
92         }
93 
94         zos.putNextEntry(new ZipEntry(zipPath));
95         Files.copy(file, zos);
96         filesAdded.add(zipPath);
97         return FileVisitResult.CONTINUE;
98       }
99     });
100 
101     return filesAdded;
102   }
103 
extractFile(String srcZip, String targetFile, String outputFilePath)104   public static void extractFile(String srcZip, String targetFile, String outputFilePath)
105       throws IOException {
106     try (OutputStream out = new FileOutputStream(outputFilePath);
107          ZipInputStream zis = new ZipInputStream(new FileInputStream(srcZip));) {
108       ZipEntry ze = zis.getNextEntry();
109       while (ze != null) {
110         if (ze.getName().equals(targetFile)) {
111           byte[] buf = new byte[1024];
112           int read = 0;
113 
114           while ((read = zis.read(buf)) > -1) {
115             out.write(buf, 0, read);
116           }
117 
118           out.close();
119           break;
120         }
121 
122         ze = zis.getNextEntry();
123       }
124     }
125   }
126 }
127