Coverage Summary for Class: Unzipper (co.rsk.db.importer.provider)
Class |
Class, %
|
Method, %
|
Line, %
|
Unzipper |
0%
(0/1)
|
0%
(0/2)
|
0%
(0/19)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2019 RSK Labs Ltd.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package co.rsk.db.importer.provider;
20
21 import java.io.*;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26
27 public class Unzipper {
28
29 /**
30 *
31 * @param sourceZipStream this parameter receives a stream of bytes with a zip file compression.
32 * This could be used to process a file or other kinds of stream
33 * @param destinationPath The path where the contents of the zip will be saved
34 * @throws IOException
35 */
36 public void unzip(InputStream sourceZipStream, Path destinationPath) throws IOException {
37 byte[] buffer = new byte[1024];
38 try (ZipInputStream zis = new ZipInputStream(sourceZipStream)) {
39 ZipEntry zipEntry = zis.getNextEntry();
40 while (zipEntry != null) {
41 Path destFile = destinationPath.resolve(zipEntry.getName());
42 if (!destFile.normalize().startsWith(destinationPath) || zipEntry.getName().contains("..")) {
43 throw new IllegalArgumentException(String.format(
44 "The source contains an illegal entry: %s", zipEntry.getName()
45 ));
46 }
47 if (zipEntry.isDirectory()) {
48 Files.createDirectory(destFile);
49 } else {
50 try (OutputStream fos = Files.newOutputStream(destFile)) {
51 for (int len = zis.read(buffer); len > 0; len = zis.read(buffer)) {
52 fos.write(buffer, 0, len);
53 }
54 }
55 }
56 zipEntry = zis.getNextEntry();
57 }
58 zis.closeEntry();
59 }
60 }
61 }