Skip to content

Commit 6d538d4

Browse files
committed
iluwatar#2 Add zip multiple files snippet
1 parent f5b1a95 commit 6d538d4

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Update the sample application with the snippet and add a test for it. After prov
2222
* [List files in directory recursively](#list-files-in-directory-recursively)
2323
* [Read lines from file to string list](#read-lines-from-file-to-string-list)
2424
* [Zip file](#zip-file)
25+
* [Zip multiple files](#zip-multiple-files)
2526

2627
### Math
2728
* [Factorial](#factorial)
@@ -182,6 +183,30 @@ Update the sample application with the snippet and add a test for it. After prov
182183
}
183184
```
184185

186+
### Zip multiple files
187+
188+
```java
189+
public static void zipFiles(String[] srcFilenames, String zipFilename) throws IOException {
190+
try (
191+
FileOutputStream fileOut = new FileOutputStream(zipFilename);
192+
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
193+
) {
194+
for (int i=0; i<srcFilenames.length; i++) {
195+
File srcFile = new File(srcFilenames[i]);
196+
try (FileInputStream fileIn = new FileInputStream(srcFile)) {
197+
ZipEntry zipEntry = new ZipEntry(srcFile.getName());
198+
zipOut.putNextEntry(zipEntry);
199+
final byte[] bytes = new byte[1024];
200+
int length;
201+
while ((length = fileIn.read(bytes)) >= 0) {
202+
zipOut.write(bytes, 0, length);
203+
}
204+
}
205+
}
206+
}
207+
}
208+
```
209+
185210
[⬆ back to top](#table-of-contents)
186211

187212
## Math

src/main/java/Library.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,32 @@ public static void zipFile(String srcFilename, String zipFilename) throws IOExce
243243
}
244244
}
245245

246+
/**
247+
* Zip multiples files
248+
* @param srcFilenames array of source file names
249+
* @param zipFilename the filename of the destination zip file
250+
* @throws IOException
251+
*/
252+
public static void zipFiles(String[] srcFilenames, String zipFilename) throws IOException {
253+
try (
254+
FileOutputStream fileOut = new FileOutputStream(zipFilename);
255+
ZipOutputStream zipOut = new ZipOutputStream(fileOut);
256+
) {
257+
for (int i=0; i<srcFilenames.length; i++) {
258+
File srcFile = new File(srcFilenames[i]);
259+
try (FileInputStream fileIn = new FileInputStream(srcFile)) {
260+
ZipEntry zipEntry = new ZipEntry(srcFile.getName());
261+
zipOut.putNextEntry(zipEntry);
262+
final byte[] bytes = new byte[1024];
263+
int length;
264+
while ((length = fileIn.read(bytes)) >= 0) {
265+
zipOut.write(bytes, 0, length);
266+
}
267+
}
268+
}
269+
}
270+
}
271+
246272
/**
247273
* Sort an array with quicksort algorithm
248274
* @param arr array to sort

src/test/java/LibraryTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ public void testIsPalindrome() {
195195
@Test
196196
public void testListFilesInDirectory() {
197197
File[] files = Library.listFilesInDirectory(new File("src/test/resources"));
198-
assertEquals(1, files.length);
198+
assertEquals(2, files.length);
199199
assertEquals("src/test/resources/somelines.txt", files[0].toString());
200+
assertEquals("src/test/resources/someotherlines.txt", files[1].toString());
200201
}
201202

202203
/**
@@ -205,7 +206,7 @@ public void testListFilesInDirectory() {
205206
@Test
206207
public void testListAllFiles() {
207208
List<File> files = Library.listAllFiles("src/test/resources");
208-
assertEquals(3, files.size());
209+
assertEquals(4, files.size());
209210
}
210211

211212
/**
@@ -238,6 +239,21 @@ public void testZipFile() throws IOException {
238239
}
239240
}
240241

242+
/**
243+
* Tests for {@link Library#zipFiles(String[], String)}
244+
*/
245+
@Test
246+
public void testZipFiles() throws IOException {
247+
final String[] srcFilenames = {"src/test/resources/somelines.txt", "src/test/resources/someotherlines.txt"};
248+
final String dst = "src/test/resources/multiple.zip";
249+
try {
250+
Library.zipFiles(srcFilenames, dst);
251+
assertTrue(Files.exists(Paths.get(dst)));
252+
} finally {
253+
Files.deleteIfExists(new File(dst).toPath());
254+
}
255+
}
256+
241257
/**
242258
* Tests for {@link Library#quickSort(int[], int, int)}
243259
*/

src/test/resources/someotherlines.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
123
2+
XYZ

0 commit comments

Comments
 (0)