-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFileManager.java
More file actions
30 lines (26 loc) · 1.02 KB
/
ZipFileManager.java
File metadata and controls
30 lines (26 loc) · 1.02 KB
1
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
28
29
30
package com.javarush.task.task31.task3110;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFileManager {
private Path zipFile;
public ZipFileManager(Path zipFile) {
this.zipFile = zipFile;
}
public void createZip(Path source) throws Exception{
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFile));
InputStream inputStream = Files.newInputStream(source))
{
ZipEntry zipEntry = new ZipEntry(source.toAbsolutePath().getFileName().toString());
zipOutputStream.putNextEntry(zipEntry);
while (inputStream.available() > 0){
byte[] cache = new byte[1024];
int length = inputStream.read(cache, 0, cache.length);
zipOutputStream.write(cache, 0, length);
}
zipOutputStream.closeEntry();
}
}
}