*.zip 파일 유틸리티 ZIP 추가

master
mjkhan21 1 year ago
parent 93e2e672f2
commit e394b23be8

@ -11,18 +11,34 @@ import java.util.zip.ZipOutputStream;
import cokr.xit.foundation.Assert;
/** /
* <p> *.zip , *.zip .
* @author mjkhan
*/
public class ZIP {
/** zip .
* @param zipPath zip
* @param targets
* @return zip
*/
public File compress(String zipPath, String... targets) {
return compress(zipPath, Arrays.asList(targets));
}
/** zip .
* @param zipPath zip
* @param targets
* @return zip
*/
public File compress(String zipPath, List<String> targets) {
try (
FileOutputStream fout = new FileOutputStream(zipPath);
ZipOutputStream zout = new ZipOutputStream(fout);
) {
for (String path: targets)
compress(zout, path);
for (String path: targets) {
File file = new File(path);
compress(zout, file, file.getName());
}
return new File(zipPath);
} catch(Exception e) {
@ -30,10 +46,20 @@ public class ZIP {
}
}
private void compress(ZipOutputStream zout, String path) throws Exception {
File file = new File(path);
private void compress(ZipOutputStream zout, File file, String filename) throws Exception {
if (file.isHidden()) return;
if (file.isDirectory()) {
zout.putNextEntry(new ZipEntry(filename.endsWith("/") ? filename : filename + "/"));
zout.closeEntry();
File[] children = file.listFiles();
for (File child: children) {
compress(zout, child, filename + "/" + child.getName());
}
} else
try (FileInputStream fis = new FileInputStream(file);) {
zout.putNextEntry(new ZipEntry(file.getName()));
zout.putNextEntry(new ZipEntry(filename));
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
@ -42,6 +68,10 @@ public class ZIP {
}
}
/** zip .
* @param zipPath zip
* @param destDir
*/
public void decompress(String zipPath, String destDir) {
try (
FileInputStream fis = new FileInputStream(zipPath);

Loading…
Cancel
Save