Zipprogram
Zipprogram
import java.io.IOException;
package zipperprogram;
import java.io.*;
import java.util.zip.*;
while (true) {
// Hiển thị menu
System.out.println("======== Zipper Program ========");
System.out.println("1. Compression");
System.out.println("2. Extraction");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
if (option.equals("1")) {
// Gọi hàm nén
compress(reader);
} else if (option.equals("2")) {
// Gọi hàm giải nén
extract(reader);
} else if (option.equals("3")) {
System.out.println("Exiting program...");
break;
} else {
System.out.println("Your choice is not valid, please choose
again.");
}
}
}
if (file.isDirectory()) {
if (fileName.endsWith("/")) {
zos.putNextEntry(new ZipEntry(fileName));
zos.closeEntry();
} else {
zos.putNextEntry(new ZipEntry(fileName + "/"));
zos.closeEntry();
}
File[] children = file.listFiles();
if (children != null) {
for (File childFile : children) {
addToZip(childFile, fileName + "/" + childFile.getName(), zos);
}
}
} else {
try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
}
}
}