Read a zip file inside zip file

后端 未结 4 1414
长情又很酷
长情又很酷 2020-12-11 16:14

I have zip file which is inside a folder in zip file please suggest me how to read it using zip input stream.

E.G.:

abc.zip
    |
      documents/b         


        
4条回答
  •  甜味超标
    2020-12-11 16:47

    I have written a code that can unzip all zip files inside a zip file. It can even unzip to n levels of compression. Like for example if you had a zip file inside a zip, inside a zip ( and so on) it would extract all of them. Use the zipFileExtract method of this class and pass the source zip file and destination directory as an argument.

    import java.io.*;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.*;
    import java.util.concurrent.ConcurrentLinkedQueue;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class RecursiveFileExtract {
        private static final int BUFFER_SIZE = 4096;
        private static Queue current;
        private static List visited;
    
        public static void zipFileExtract(File sourceZipFile, File destinationDirectory) {
            Path temp = null;
            if(!destinationDirectory.exists())
            {
                destinationDirectory.mkdirs();
            }
            try {
                temp = Files.move(Paths.get(sourceZipFile.getAbsolutePath()), Paths.get(destinationDirectory.getAbsolutePath()+File.separator+sourceZipFile.getName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            File zipFile = new File(temp.toAbsolutePath().toString());
            current = new ConcurrentLinkedQueue<>();
            visited = new ArrayList<>();
            current.add(zipFile);
            do {
                unzipCurrent();
                zipFinder(destinationDirectory);
            }
            while (!current.isEmpty());
        }
    
        private static void zipFinder(File directory) {
            try {
                if (directory != null) {
                    File fileArray[] = directory.listFiles();
                    if (fileArray != null) {
                        for (File file : fileArray) {
                            if (file != null) {
                                if (file.isDirectory()) {
                                    zipFinder(file);
                                } else {
                                    if (file.getName().endsWith(".zip")) {
                                        if (!visited.contains(file)) {
                                            current.add(file);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    
        private static void unzipCurrent() {
            try {
                while (!current.isEmpty()) {
                    File file = current.remove();
                    visited.add(file);
                    File zipDirectory = new File(file.getParentFile().getAbsolutePath());
                    unzip(file.getAbsolutePath(), zipDirectory.getAbsolutePath());
                }
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    
        public static void unzip(String zipFilePath, String destDirectory) {
            try {
                ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
                ZipEntry entry = zipIn.getNextEntry();
    
                while (entry != null) {
                    String filePath = destDirectory + File.separator + entry.getName();
                    if (!entry.isDirectory()) {
                        extractFile(zipIn, filePath);
                    } else {
    
                        File dir = new File(filePath);
                        Boolean result = dir.mkdir();
                    }
                    zipIn.closeEntry();
                    entry = zipIn.getNextEntry();
                }
                zipIn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void extractFile(ZipInputStream zipIn, String filePath) {
            try {
                File file = new File(filePath);
                File parentFile = file.getParentFile();
                if (!parentFile.exists()) {
                    Boolean result = parentFile.mkdirs();
                    if (!result) {
                        throw new Exception();
                    }
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
                byte[] bytesIn = new byte[BUFFER_SIZE];
                int read = 0;
                while ((read = zipIn.read(bytesIn)) != -1) {
                    bos.write(bytesIn, 0, read);
                }
                bos.close();
            } catch (Exception e) {
               System.out.println(e.getLocalizedMessage());
            }
        }
    }
    

提交回复
热议问题