K
K
Katya Ivanova2022-04-15 22:06:41
Java
Katya Ivanova, 2022-04-15 22:06:41

How to read from a zip archive?

Hello ! There are folders and files in my zip archive, only 1 file is read, how to count all files and folders?

PS - I am writing a launcher for minecraft :D
Here is my code.

public static void unZip(String arhiv, String outDir){
        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(arhiv));

            String name;
            long size;
            ZipEntry entry;

            while ((entry = zis.getNextEntry()) != null){
                name = entry.getName();
                size = entry.getSize();

                System.out.println("Получены файлы: " + name + ", размером - " + size + " ; ");

                FileOutputStream out = new FileOutputStream(outDir + name);

                for (int c = zis.read(); c != -1; c++){
                    out.write(c);
                }
                out.flush();
                zis.closeEntry();
                out.close();
            }
        } catch (IOException e){
            e.printStackTrace();
        }


Here is the contents of the archive that I want to read.

6259bbf292df2828793376.jpeg

6259bbfb89b5a317382993.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheAndrey7, 2022-04-15
@Kats555

for (int c = zis.read(); c != -1; c++){
    out.write(c);
}

Yes, you will unpack the archive for half a day one bit at a time. This is done through a 4096 bit byte buffer. In general, it's better to look at the methods of the java.nio.Files class and study the AutoCloseable topic for beautiful try-catch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question