解压到目标目录

时间:2019-12-23 12:36:36

标签: android unzip

我想解压缩到同一目录中的zip文件,当我尝试按以下代码将其解压缩到目标目录下而不是目标文件夹下时,如何解压缩android目标目录中的所有文件

public static void unzip(String zipFilePath, String destDir) {
    BufferedOutputStream bufferedOutputStream = null;
    FileInputStream fileInputStream;

    File dest_file = new File(destDir);
    //dest_file.mkdirs(); // creates if destination directory not existed

    try {
        fileInputStream = new FileInputStream(zipFilePath);
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
        ZipEntry zipEntry;

        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String zipEntryName = zipEntry.getName();
            File file = new File(destDir + zipEntryName);

            if (file.exists()) {

            } else if (zipEntry.isDirectory()) {
                file.mkdirs();
            } else {
                byte buffer[] = new byte[1024];
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream, 1024);
                int count;

                while ((count = zipInputStream.read(buffer, 0, 1024)) != -1) {
                    bufferedOutputStream.write(buffer, 0, count);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
            }
        }
        zipInputStream.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }

}

我叫解压缩文件

 String source = /storage/emulated/0/Android/data/com.kocsistem.pixageoneandroid/files/Contents/widgetContent  + "/" + "123.zip";
 String target = /storage/emulated/0/Android/data/com.kocsistem.pixageoneandroid/files/Contents/widgetContent ;
 Utils.unzip(source, target);

0 个答案:

没有答案