forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipHelper.java
More file actions
95 lines (85 loc) · 3.38 KB
/
Copy pathZipHelper.java
File metadata and controls
95 lines (85 loc) · 3.38 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
/*
* Copyright (C) 2004-2017, GoodData(R) Corporation. All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
package com.gooddata.util;
import static com.gooddata.util.Validate.notNull;
import org.springframework.util.StreamUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* Utility class for manipulating zip archives.
*/
public abstract class ZipHelper {
/**
* This method compresses the input file to zip format. If the given file is a directory, it recursively
* packs the directory into the output. Not including given directory itself.
* If the given file is already zipped, simply copies it into the output.
*
* @param file file to be zipped
* @param output stream where the output will be written
* @throws IOException if zip creation fails
*/
public static void zip(File file, OutputStream output) throws IOException {
zip(file, output, false);
}
/**
* This method compresses the input file to zip format. If the given file is a directory, it recursively
* packs the directory into the output. If the given file is already zipped, simply copies it into the output.
*
* @param file file to be zipped
* @param output stream where the output will be written
* @param includeRoot if root dir should be included
* @throws IOException if zip creation fails
*/
public static void zip(File file, OutputStream output, boolean includeRoot) throws IOException {
notNull(file, "file");
notNull(output, "output");
if (isZipped(file)) {
try (InputStream fis = Files.newInputStream(file.toPath())) {
StreamUtils.copy(fis, output);
}
} else {
try (ZipOutputStream zos = new ZipOutputStream(output)) {
if (file.isDirectory()) {
zipDir(includeRoot ? file.getParentFile().toPath() : file.toPath(), file, zos);
} else {
zipFile(file.getParentFile().toPath(), file, zos);
}
}
}
}
private static void zipDir(Path rootPath, File dir, ZipOutputStream zos) throws IOException {
for (File file : notNull(dir.listFiles(), "listed files")) {
if (file.isDirectory()) {
zipDir(rootPath, file, zos);
} else {
zipFile(rootPath, file, zos);
}
}
}
private static void zipFile(Path rootPath, File file, ZipOutputStream zos) throws IOException {
ZipEntry ze = new ZipEntry(rootPath.relativize(file.toPath()).toString());
zos.putNextEntry(ze);
try (InputStream fis = Files.newInputStream(file.toPath())) {
StreamUtils.copy(fis, zos);
}
zos.closeEntry();
}
private static boolean isZipped(File file) {
try (final InputStream stream = Files.newInputStream(file.toPath());
final ZipInputStream zipStream = new ZipInputStream(stream)) {
return zipStream.getNextEntry() != null;
} catch (IOException e) {
return false;
}
}
}