【问题标题】:getting issue in unzipping folders - files from folder to folder解压缩文件夹时遇到问题 - 从文件夹到文件夹的文件
【发布时间】:2017-11-03 05:37:24
【问题描述】:

下面是我将文件夹解压到目标文件夹的方法和方法定义

unzip(filepath, unzipLocation);

这是我解压缩 zip 文件的方法,此方法适用于文件,但是当我的 zip 文件像 a.zip 时出现问题,其中包含文件夹 2 文件夹(即 abc1,abc2),并且文件夹 abc1 有文件夹并且它有文件请帮帮我

private void unzip(String src, String dest) {
        String _location = "";
        final int BUFFER_SIZE = 4096;
        _location = dest;

        System.out.println("_location :::  " + dest + "");
        System.out.println("src :::  " + src + "");
        BufferedOutputStream bufferedOutputStream = null;
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(src);
            ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
            ZipEntry zipEntry;

            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                String zipEntryName = zipEntry.getName();
                String name = dest.substring(dest.lastIndexOf("/") - 1);
                //  System.out.println("NAME "+name);
//                File FileName = new File(FolderName);
                File FileName = new File(_location.toString());
                if (!FileName.isDirectory()) {
                    try {
                        if (FileName.mkdir()) {
                        } else {
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                String loc = "";
                String fname = "";
                String LOC = "";
                LOC = _location.toString();
//                File file = new File(FolderName+"/" +zipEntryName);
                System.out.println("ZIP " + zipEntryName + "");
                //   zipEntryName=zipEntryName.r
                if (zipEntryName.contains("/")) {

                    String[] file = zipEntryName.split("/");
                    System.out.println("CHECK ::: " + file.length);

                    System.out.println("CHECK ::: " + file[0] + "");
                    String test = zipEntryName.substring(zipEntryName.lastIndexOf("/"), zipEntryName.length());
                    System.out.println("TEST " + test + "");
                    if (test.length() > 1) {
                        LOC = "";
//                        zipEntryName = file[1];
                        zipEntryName = file[file.length - 1];
                        System.out.println("ZIP UPDATED " + zipEntryName + "");
                        //    _location=_location+"/"+file[0]+"/";

                        String l = "";
                        for (int i = 0; i < file.length - 1; i++) {
                            l = l + "/" + file[i];

                        }
//                        System.out.println("TESTTTTTTTT " + l + "");
//                        loc = _location + "/" + file[0];
                        //    loc = _location + "/" + l;
                        LOC = _location + "/" + l;

                        File thumb = new File(LOC);
                        if (!thumb.exists()) {
                            thumb.mkdir();
                        }

                        System.out.println("createddd dir ");

                        System.out.println("createddd dir loc " + loc);
                    } else {
                        System.out.println("create dir ");

                        System.out.println("HERE _location111: : : :  " + _location);
                        System.out.println("HERE zipEntryName1111 : : : :  /" + zipEntryName);

//                        File thumb = new File(_location+"/"+zipEntryName);
                        File thumb = new File(LOC + "/" + zipEntryName);
                        if (!thumb.exists()) {
                            thumb.mkdir();
                        }
                    }
                                  }

                System.out.println("HERE _location: : : :  " + _location);
                System.out.println("HERE zipEntryName : : : :  /" + zipEntryName);
                System.out.println("HERE loc : : : :  /" + loc);
//                File file = new File(_location + "/" + zipEntryName);
                File file = new File(LOC + "/" + zipEntryName);
                if (file.exists()) {

                } else {
                    if (zipEntry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        byte buffer[] = new byte[BUFFER_SIZE];
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
                        int count;
                        while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
                            bufferedOutputStream.write(buffer, 0, count);
                        }
                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    }
                }
            }
            zipInputStream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

【问题讨论】:

    标签: java android unzip


    【解决方案1】:

    public static void unZipIt(String zipFile, String outputFolder) {

        byte[] buffer = new byte[1024];
        //mm    System.out.println("ZIP FILE "+zipFile+"");
        //mm   System.out.println("outputFolder FILE "+outputFolder+"");
        try {
    
            //create output directory is not exists
            File folder = new File(outputFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }
    
            //get the zip file content
            ZipInputStream zis =
                    new ZipInputStream(new FileInputStream(zipFile));
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();
    
            while (ze != null) {
    
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);
    
                //mm  System.out.println("file unzip : " + newFile.getAbsoluteFile());
    
                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
    
                FileOutputStream fos = new FileOutputStream(newFile);
    
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
    
                fos.close();
                ze = zis.getNextEntry();
            }
    
            zis.closeEntry();
            zis.close();
    
            System.out.println("Done");
    
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 2017-04-18
      • 2021-10-16
      • 2016-09-17
      • 2015-04-05
      • 2010-12-14
      相关资源
      最近更新 更多