messpy / local_application

ローカル
1 stars 0 forks source link

javaコード #3

Open messpy opened 3 months ago

messpy commented 3 months ago

import java.io.; import java.util.zip.;

public class ZipExtractor {

public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Usage: java ZipExtractor <zip-file>");
        return;
    }

    String zipFilePath = args[0];
    File zipFile = new File(zipFilePath);

    if (!zipFile.exists()) {
        System.err.println("File not found: " + zipFilePath);
        return;
    }

    try {
        unzip(zipFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void unzip(File zipFile) throws IOException {
    byte[] buffer = new byte[1024];
    File destDir = new File(zipFile.getParent(), zipFile.getName().replace(".zip", ""));
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry zipEntry = zis.getNextEntry();

    int totalEntries = 0;
    while (zipEntry != null) {
        totalEntries++;
        zipEntry = zis.getNextEntry();
    }
    zis.close();

    zis = new ZipInputStream(new FileInputStream(zipFile));
    zipEntry = zis.getNextEntry();
    int processedEntries = 0;

    while (zipEntry != null) {
        File newFile = newFile(destDir, zipEntry);
        if (zipEntry.isDirectory()) {
            if (!newFile.isDirectory() && !newFile.mkdirs()) {
                throw new IOException("Failed to create directory " + newFile);
            }
        } else {
            // Fix for Windows-created archives
            File parent = newFile.getParentFile();
            if (!parent.isDirectory() && !parent.mkdirs()) {
                throw new IOException("Failed to create directory " + parent);
            }

            // Write file content
            try (FileOutputStream fos = new FileOutputStream(newFile)) {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            }
        }

        processedEntries++;
        printProgress(processedEntries, totalEntries);

        zipEntry = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

private static File newFile(File destDir, ZipEntry zipEntry) throws IOException {
    File destFile = new File(destDir, zipEntry.getName());

    String destDirPath = destDir.getCanonicalPath();
    String destFilePath = destFile.getCanonicalPath();

    if (!destFilePath.startsWith(destDirPath + File.separator)) {
        throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());
    }

    return destFile;
}

private static void printProgress(int processedEntries, int totalEntries) {
    int progress = (int) ((processedEntries / (double) totalEntries) * 100);
    StringBuilder progressBar = new StringBuilder("[");
    for (int i = 0; i < 50; i++) {
        if (i < (progress / 2)) {
            progressBar.append("#");
        } else {
            progressBar.append(" ");
        }
    }
    progressBar.append("] ");
    progressBar.append(progress).append("%");
    System.out.print("\r" + progressBar.toString());
}

}