Skip to content

Commit 0d4b954

Browse files
committed
Now there is the service that zips a list of files
1 parent 9858058 commit 0d4b954

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.mbenincasa.javaexcelutils.model.zip;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
/**
8+
* This is a model which contains the data of the file to be zipped which is used in {@code ZipUtility}
9+
* @author Mirko Benincasa
10+
* @since 0.4.2
11+
*/
12+
@AllArgsConstructor
13+
@Getter
14+
@Setter
15+
public class ZipModel {
16+
17+
/**
18+
* The Byte array of the file to zip
19+
*/
20+
private byte[] bytes;
21+
22+
/**
23+
* The name of the file to zip
24+
*/
25+
private String filename;
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.mbenincasa.javaexcelutils.tools;
2+
3+
import io.github.mbenincasa.javaexcelutils.model.zip.ZipModel;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.util.List;
8+
import java.util.zip.ZipEntry;
9+
import java.util.zip.ZipOutputStream;
10+
11+
/**
12+
* {@code ZipUtility} is a static class that provides utility methods for zipped files
13+
* @author Mirko Benincasa
14+
* @since 0.4.2
15+
*/
16+
public class ZipUtility {
17+
18+
/**
19+
* @param zipModels A list of ZipModel objects representing the files to zip
20+
* @return The zipped file in the form of a ByteArrayOutputStream
21+
* @throws IOException If an I/O error has occurred
22+
*/
23+
public static ByteArrayOutputStream zipFiles(List<ZipModel> zipModels) throws IOException {
24+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
25+
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
26+
for (ZipModel zipModel : zipModels) {
27+
ZipEntry zipEntry = new ZipEntry(zipModel.getFilename());
28+
zipEntry.setSize(zipModel.getBytes().length);
29+
zipOutputStream.putNextEntry(zipEntry);
30+
zipOutputStream.write(zipModel.getBytes());
31+
}
32+
33+
zipOutputStream.closeEntry();
34+
zipOutputStream.close();
35+
return byteArrayOutputStream;
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.mbenincasa.javaexcelutils.tools;
2+
3+
import io.github.mbenincasa.javaexcelutils.model.zip.ZipModel;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.*;
8+
import java.nio.file.Files;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.zip.ZipEntry;
12+
import java.util.zip.ZipInputStream;
13+
14+
class ZipUtilityTest {
15+
16+
private final File excelFile = new File("./src/test/resources/employee_2.xlsx");
17+
private final File jsonFile = new File("./src/test/resources/office.json");
18+
19+
@Test
20+
void zipFiles() throws IOException {
21+
byte[] bytes = Files.readAllBytes(excelFile.toPath());
22+
ZipModel zipModel = new ZipModel(bytes, excelFile.getName());
23+
byte[] bytes1 = Files.readAllBytes(jsonFile.toPath());
24+
ZipModel zipModel1 = new ZipModel(bytes1, jsonFile.getName());
25+
26+
List<ZipModel> zipModels = new ArrayList<>();
27+
zipModels.add(zipModel);
28+
zipModels.add(zipModel1);
29+
ByteArrayOutputStream byteArrayOutputStream = ZipUtility.zipFiles(zipModels);
30+
FileOutputStream fileOutputStream = new FileOutputStream("./src/test/resources/file.zip");
31+
fileOutputStream.write(byteArrayOutputStream.toByteArray());
32+
33+
fileOutputStream.close();
34+
35+
File file = new File("./src/test/resources/file.zip");
36+
FileInputStream fs = new FileInputStream(file);
37+
ZipInputStream Zs = new ZipInputStream( new BufferedInputStream(fs));
38+
39+
ZipEntry ze = Zs.getNextEntry();
40+
assert ze != null;
41+
Assertions.assertEquals("employee_2.xlsx", ze.getName());
42+
ze = Zs.getNextEntry();
43+
Assertions.assertEquals("office.json", ze.getName());
44+
45+
fs.close();
46+
file.delete();
47+
}
48+
}

0 commit comments

Comments
 (0)