1 package fr.ifremer.quadrige2.core.dao.technical;
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 import org.apache.commons.io.IOUtils;
28 import org.springframework.util.StreamUtils;
29
30 import java.io.*;
31 import java.util.Enumeration;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipFile;
34 import java.util.zip.ZipOutputStream;
35
36
37
38
39 public class ZipUtils {
40
41
42
43
44 protected ZipUtils() {
45
46 }
47
48
49
50
51
52
53
54
55
56 public static void compressFilesInPath(String path, File compressedFile, boolean deleteAfterCompress) throws IOException {
57 compressFilesInPath(new File(path), compressedFile, deleteAfterCompress);
58 }
59
60
61
62
63
64
65
66
67
68 public static void compressFilesInPath(File fileSource, File compressedFile, boolean deleteAfterCompress) throws IOException {
69 ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compressedFile)));
70 compressDirectory(fileSource, fileSource, compressedFile, zout, deleteAfterCompress);
71 zout.close();
72 }
73
74
75
76
77
78
79
80
81
82
83 public static void uncompressFileToPath(File compressedFile, String path, boolean deleteAfterUncompress) throws IOException {
84 File destDir;
85 if (path != null) {
86 destDir = new File(path);
87 }
88 else {
89 destDir = new File(compressedFile.getParent());
90 }
91 destDir.mkdirs();
92
93 ZipFile zipFile = new ZipFile(compressedFile);
94 Enumeration<? extends ZipEntry> entries = zipFile.entries();
95 while (entries.hasMoreElements()) {
96 ZipEntry zipEntry = (ZipEntry) entries.nextElement();
97 if (!zipEntry.isDirectory()) {
98 File destFile = new File(destDir, zipEntry.getName());
99 destFile.getParentFile().mkdirs();
100 OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile));
101 StreamUtils.copy(zipFile.getInputStream(zipEntry), out);
102 out.flush();
103 out.close();
104 }
105 }
106 zipFile.close();
107 if (deleteAfterUncompress) {
108 compressedFile.delete();
109 }
110 }
111
112
113
114
115 private static void compressDirectory(File rootFileSource, File fileSource, File compressedFile, ZipOutputStream zout, boolean deleteAfterCompress) throws IOException {
116 File[] files = fileSource.listFiles();
117 for (File file : files) {
118 if (file.equals(compressedFile)) {
119 continue;
120 }
121 if (file.isDirectory()) {
122 String zipEntryName = toZipEntryName(rootFileSource, file);
123 zout.putNextEntry(new ZipEntry(zipEntryName));
124 compressDirectory(rootFileSource, file, compressedFile, zout, deleteAfterCompress);
125 zout.closeEntry();
126 if (deleteAfterCompress) {
127 file.delete();
128 }
129 continue;
130 }
131 InputStream in = new BufferedInputStream(new FileInputStream(file));
132 String zipEntryName = toZipEntryName(rootFileSource, file);
133 zout.putNextEntry(new ZipEntry(zipEntryName));
134 IOUtils.copy(in, zout);
135 zout.closeEntry();
136 in.close();
137 if (deleteAfterCompress) {
138 file.delete();
139 }
140 }
141 }
142
143 private static String toZipEntryName(File root, File file) {
144 String result = file.getPath();
145 if(root != null) {
146 String rootPath = root.getPath();
147 if(result.startsWith(rootPath)) {
148 result = result.substring(rootPath.length());
149 }
150 }
151
152 result = result.replace('\\', '/');
153 if(file.isDirectory()) {
154 result = result + '/';
155 }
156
157 while(result.startsWith("/")) {
158 result = result.substring(1);
159 }
160
161 return result;
162 }
163 }