Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d538d4

Browse files
committed
iluwatar#2 Add zip multiple files snippet
1 parentf5b1a95 commit6d538d4

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+
publicstaticvoid zipFiles(String[] srcFilenames,String zipFilename) throwsIOException {
190+
try (
191+
FileOutputStream fileOut=newFileOutputStream(zipFilename);
192+
ZipOutputStream zipOut=newZipOutputStream(fileOut);
193+
) {
194+
for (int i=0; i<srcFilenames.length; i++) {
195+
File srcFile=newFile(srcFilenames[i]);
196+
try (FileInputStream fileIn=newFileInputStream(srcFile)) {
197+
ZipEntry zipEntry=newZipEntry(srcFile.getName());
198+
zipOut.putNextEntry(zipEntry);
199+
finalbyte[] bytes=newbyte[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+
publicstaticvoidzipFiles(String[]srcFilenames,StringzipFilename)throwsIOException {
253+
try (
254+
FileOutputStreamfileOut =newFileOutputStream(zipFilename);
255+
ZipOutputStreamzipOut =newZipOutputStream(fileOut);
256+
) {
257+
for (inti=0;i<srcFilenames.length;i++) {
258+
FilesrcFile =newFile(srcFilenames[i]);
259+
try (FileInputStreamfileIn =newFileInputStream(srcFile)) {
260+
ZipEntryzipEntry =newZipEntry(srcFile.getName());
261+
zipOut.putNextEntry(zipEntry);
262+
finalbyte[]bytes =newbyte[1024];
263+
intlength;
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
publicvoidtestListFilesInDirectory() {
197197
File[]files =Library.listFilesInDirectory(newFile("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
publicvoidtestListAllFiles() {
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+
publicvoidtestZipFiles()throwsIOException {
247+
finalString[]srcFilenames = {"src/test/resources/somelines.txt","src/test/resources/someotherlines.txt"};
248+
finalStringdst ="src/test/resources/multiple.zip";
249+
try {
250+
Library.zipFiles(srcFilenames,dst);
251+
assertTrue(Files.exists(Paths.get(dst)));
252+
}finally {
253+
Files.deleteIfExists(newFile(dst).toPath());
254+
}
255+
}
256+
241257
/**
242258
* Tests for {@link Library#quickSort(int[], int, int)}
243259
*/
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)

[8]ページ先頭

©2009-2025 Movatter.jp