0% found this document useful (0 votes)
8 views3 pages

Zipprogram

Code chương trình zip and unzip

Uploaded by

dothhe182087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Zipprogram

Code chương trình zip and unzip

Uploaded by

dothhe182087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package zipperprogram;

import java.io.IOException;

public class ZipperProgram {


public static void main(String[] args) throws IOException {
ZipperMethod zipper = new ZipperMethod();
zipper.menu();
}
}

package zipperprogram;
import java.io.*;
import java.util.zip.*;

public class ZipperMethod {

public void menu() throws IOException {


BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));

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: ");

String option = reader.readLine();

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.");
}
}
}

// Hàm nén file


public void compress(BufferedReader reader) throws IOException {
System.out.println("--------- Compression ---------");
System.out.print("Enter source folder path: ");
String sourcePath = reader.readLine();

System.out.print("Enter name file zip (Example: namefile.zip): ");


String zipFileName = reader.readLine();
try (FileOutputStream fos = new FileOutputStream(zipFileName);
ZipOutputStream zos = new ZipOutputStream(fos)) {

File sourceFile = new File(sourcePath);


addToZip(sourceFile, sourceFile.getName(), zos);

System.out.println("Zip file successfully");


} catch (Exception e) {
System.out.println("Error during compression: " + e.getMessage());
}
}

// Hàm thêm file vào zip


private void addToZip(File file, String fileName, ZipOutputStream zos) throws
IOException {
if (file.isHidden()) return;

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);
}
}
}
}

// Hàm giải nén file


public void extract(BufferedReader reader) throws IOException {
System.out.println("--------- Extraction ---------");
System.out.print("Nhập đường dẫn file zip nguồn: ");
String zipFilePath = reader.readLine();

System.out.print("Nhập thư mục đích để giải nén: ");


String destDir = reader.readLine();

File destFileDir = new File(destDir);


if (!destFileDir.exists()) {
destFileDir.mkdir();
}

try (FileInputStream fis = new FileInputStream(zipFilePath);


ZipInputStream zis = new ZipInputStream(fis)) {

ZipEntry entry = zis.getNextEntry();


while (entry != null) {
File newFile = new File(destDir, entry.getName());
if (entry.isDirectory()) {
newFile.mkdirs();
} else {
// Đảm bảo thư mục con tồn tại
new File(newFile.getParent()).mkdirs();
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[1024];
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
entry = zis.getNextEntry();
}
System.out.println("Giải nén thành công!");
} catch (Exception e) {
System.out.println("Lỗi trong quá trình giải nén: " + e.getMessage());
}
}
}

You might also like