在解压缩文件夹中遇到问题 - 从文件夹到文件夹的文件

时间:2017-11-03 05:37:24

标签: java android unzip

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

unzip(filepath, unzipLocation);

这是我解压缩文件的方法这个方法工作文件但是当我的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();
        }
    }

1 个答案:

答案 0 :(得分: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();
    }
}
相关问题