ZipUtil.java
6.69 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.sling.api.resource.Resource
*/
package com.adobe.cq.wcm.translation.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
public class ZipUtil {
public static void zipDirectory(File directoryToZip, String strOutputFilePath) throws FileNotFoundException, IOException {
ArrayList<File> fileList = new ArrayList<File>();
ZipUtil.getAllFiles(directoryToZip, fileList, true);
ZipUtil.writeZipFile(directoryToZip, fileList, strOutputFilePath);
}
public static void getAllFiles(File dir, List<File> fileList, boolean bAddDirectory) {
File[] files;
for (File file : files = dir.listFiles()) {
if (bAddDirectory || file.isFile()) {
fileList.add(file);
}
if (!file.isDirectory()) continue;
ZipUtil.getAllFiles(file, fileList, bAddDirectory);
}
}
private static void writeZipFile(File directoryToZip, List<File> fileList, String strZipFilePath) throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream(strZipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : fileList) {
if (file.isDirectory()) continue;
ZipUtil.addToZip(directoryToZip, file, zos);
}
zos.close();
fos.close();
}
private static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
int length;
FileInputStream fis = new FileInputStream(file);
String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1, file.getCanonicalPath().length());
ZipEntry zipEntry = new ZipEntry(zipFilePath);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Loose catch block
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
public static boolean isValidFile(File file) {
boolean bValid = false;
ZipFile zipfile = null;
zipfile = new ZipFile(file);
bValid = true;
try {
if (zipfile == null) return bValid;
zipfile.close();
return bValid;
}
catch (IOException e) {
return bValid;
}
catch (ZipException e) {
try {
if (zipfile == null) return bValid;
zipfile.close();
return bValid;
}
catch (IOException e) {
return bValid;
}
catch (IOException e2) {
try {
if (zipfile == null) return bValid;
zipfile.close();
return bValid;
}
catch (IOException e) {
return bValid;
}
catch (Throwable throwable) {
try {
if (zipfile == null) throw throwable;
zipfile.close();
zipfile = null;
throw throwable;
}
catch (IOException e) {
// empty catch block
}
throw throwable;
}
}
}
}
private static boolean unZipToDir(InputStream fis, String destDir, long maxFileUnzipSize) throws IOException {
boolean bContinue = true;
long totalFileSize = 0;
File destDirFile = new File(destDir);
if (!destDirFile.exists()) {
destDirFile.mkdirs();
}
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while (ze != null && bContinue) {
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
if (newFile.getAbsolutePath().indexOf(destDir) == 0 && newFile.getAbsolutePath().indexOf("..") == -1) {
new File(newFile.getParent()).mkdirs();
if (ze.isDirectory()) {
newFile.mkdir();
} else {
int len;
FileOutputStream fos = new FileOutputStream(newFile);
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
if ((totalFileSize += newFile.length()) > maxFileUnzipSize && maxFileUnzipSize > 0) {
bContinue = false;
break;
}
}
}
zis.closeEntry();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
return bContinue;
}
public static boolean unZipToDir(String zipFilePath, String destDir, long maxFileUnzipSize) throws IOException {
boolean bRetVal = false;
FileInputStream fis = new FileInputStream(zipFilePath);
bRetVal = ZipUtil.unZipToDir(fis, destDir, maxFileUnzipSize);
fis.close();
return bRetVal;
}
public static boolean unZipToDir(Resource zipNode, String destDir, int maxFileUnzipSize) throws RepositoryException, IOException {
Node contentNode;
Property prop;
Resource zipContent = zipNode.getChild("jcr:content");
boolean bRetVal = false;
if (zipContent != null && (prop = (contentNode = (Node)zipContent.adaptTo(Node.class)).getProperty("jcr:data")) != null && prop.getType() == 2) {
bRetVal = ZipUtil.unZipToDir(prop.getBinary().getStream(), destDir, (long)maxFileUnzipSize);
}
return bRetVal;
}
}