package zipperprogram;
import [Link];
public class ZipperProgram {
public static void main(String[] args) throws IOException {
ZipperMethod zipper = new ZipperMethod();
[Link]();
}
}
package zipperprogram;
import [Link].*;
import [Link].*;
public class ZipperMethod {
public void menu() throws IOException {
BufferedReader reader = new BufferedReader(new
InputStreamReader([Link]));
while (true) {
// Hiển thị menu
[Link]("======== Zipper Program ========");
[Link]("1. Compression");
[Link]("2. Extraction");
[Link]("3. Exit");
[Link]("Enter your choice: ");
String option = [Link]();
if ([Link]("1")) {
// Gọi hàm nén
compress(reader);
} else if ([Link]("2")) {
// Gọi hàm giải nén
extract(reader);
} else if ([Link]("3")) {
[Link]("Exiting program...");
break;
} else {
[Link]("Your choice is not valid, please choose
again.");
}
}
}
// Hàm nén file
public void compress(BufferedReader reader) throws IOException {
[Link]("--------- Compression ---------");
[Link]("Enter source folder path: ");
String sourcePath = [Link]();
[Link]("Enter name file zip (Example: [Link]): ");
String zipFileName = [Link]();
try (FileOutputStream fos = new FileOutputStream(zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos)) {
File sourceFile = new File(sourcePath);
addToZip(sourceFile, [Link](), zos);
[Link]("Zip file successfully");
} catch (Exception e) {
[Link]("Error during compression: " + [Link]());
}
}
// Hàm thêm file vào zip
private void addToZip(File file, String fileName, ZipOutputStream zos) throws
IOException {
if ([Link]()) return;
if ([Link]()) {
if ([Link]("/")) {
[Link](new ZipEntry(fileName));
[Link]();
} else {
[Link](new ZipEntry(fileName + "/"));
[Link]();
}
File[] children = [Link]();
if (children != null) {
for (File childFile : children) {
addToZip(childFile, fileName + "/" + [Link](), zos);
}
}
} else {
try (FileInputStream fis = new FileInputStream(file)) {
ZipEntry zipEntry = new ZipEntry(fileName);
[Link](zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = [Link](bytes)) >= 0) {
[Link](bytes, 0, length);
}
}
}
}
// Hàm giải nén file
public void extract(BufferedReader reader) throws IOException {
[Link]("--------- Extraction ---------");
[Link]("Nhập đường dẫn file zip nguồn: ");
String zipFilePath = [Link]();
[Link]("Nhập thư mục đích để giải nén: ");
String destDir = [Link]();
File destFileDir = new File(destDir);
if (![Link]()) {
[Link]();
}
try (FileInputStream fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(fis)) {
ZipEntry entry = [Link]();
while (entry != null) {
File newFile = new File(destDir, [Link]());
if ([Link]()) {
[Link]();
} else {
// Đảm bảo thư mục con tồn tại
new File([Link]()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[1024];
int len;
while ((len = [Link](buffer)) > 0) {
[Link](buffer, 0, len);
}
}
}
entry = [Link]();
}
[Link]("Giải nén thành công!");
} catch (Exception e) {
[Link]("Lỗi trong quá trình giải nén: " + [Link]());
}
}
}