-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Expand file tree
/
Copy pathZip.java
More file actions
158 lines (139 loc) · 5.5 KB
/
Zip.java
File metadata and controls
158 lines (139 loc) · 5.5 KB
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
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.io;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.jspecify.annotations.Nullable;
public class Zip {
private static final Logger LOG = Logger.getLogger(Zip.class.getName());
private static final int BUF_SIZE = 16384; // "big"
public static String zip(File input) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
try (ZipOutputStream zos = new ZipOutputStream(bos)) {
if (input.isDirectory()) {
addToZip(input.getAbsolutePath(), zos, input);
} else {
addToZip(input.getParentFile().getAbsolutePath(), zos, input);
}
}
return Base64.getEncoder().encodeToString(bos.toByteArray());
}
}
private static void addToZip(String basePath, ZipOutputStream zos, File toAdd)
throws IOException {
Path dirPath = toAdd.toPath();
if (Files.isDirectory(dirPath)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath)) {
for (Path path : stream) {
addToZip(basePath, zos, path.toFile());
}
} catch (IOException e) {
LOG.warning(() -> String.format("Failed to read directory %s for zipping: %s", toAdd, e));
}
} else {
try (FileInputStream fis = new FileInputStream(toAdd)) {
String name = toAdd.getAbsolutePath().substring(basePath.length() + 1);
ZipEntry entry = new ZipEntry(name.replace('\\', '/'));
entry.setTime(toAdd.lastModified());
entry.setLastModifiedTime(FileTime.fromMillis(toAdd.lastModified()));
zos.putNextEntry(entry);
int len;
byte[] buffer = new byte[4096];
while ((len = fis.read(buffer)) != -1) {
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
}
}
public static File unzipToTempDir(String source, String prefix, String suffix)
throws IOException {
File output = TemporaryFilesystem.getDefaultTmpFS().createTempDir(prefix, suffix);
Zip.unzip(source, output);
return output;
}
public static void unzip(String source, File outputDir) throws IOException {
byte[] bytes = Base64.getMimeDecoder().decode(source);
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes)) {
unzip(bis, outputDir);
}
}
public static File unzipToTempDir(InputStream source, String prefix, String suffix)
throws IOException {
File output = TemporaryFilesystem.getDefaultTmpFS().createTempDir(prefix, suffix);
Zip.unzip(source, output);
return output;
}
public static void unzip(InputStream source, File outputDir) throws IOException {
try (ZipInputStream zis = new ZipInputStream(source)) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
File file = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
FileHandler.createDir(file);
continue;
}
unzipFile(outputDir, zis, entry.getName());
setLastModified(file, entry.getLastModifiedTime());
}
}
}
private static void setLastModified(File file, @Nullable FileTime time) {
if (time != null) {
boolean ok = file.setLastModified(time.toMillis());
if (!ok) {
LOG.log(
Level.WARNING,
() -> String.format("Failed to set last modified %s for file %s", time, file));
}
}
}
public static void unzipFile(File output, InputStream zipStream, String name) throws IOException {
String canonicalDestinationDirPath = output.getCanonicalPath();
File toWrite = new File(output, name);
String canonicalDestinationFile = toWrite.getCanonicalPath();
if (!canonicalDestinationFile.startsWith(canonicalDestinationDirPath + File.separator)) {
throw new IOException("Entry is outside of the target dir: " + name);
}
if (!FileHandler.createDir(toWrite.getParentFile()))
throw new IOException("Cannot create parent directory for: " + name);
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toWrite), BUF_SIZE)) {
byte[] buffer = new byte[BUF_SIZE];
int read;
while ((read = zipStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
}