如何从内部存储中提取一个zip文件?

时间:2018-11-01 10:48:12

标签: java android

我想做一个下载文件并解压缩文件的项目。我尝试修复了这么长时间。请查看我的代码并为我提供帮助,或者有人告诉我如何下载文件和Extrack zip文件。 在文件“ download.zip”中,包含5个视频文件。 我使用Sreedev R

中的类解压缩
public class MainActivity extends AppCompatActivity {

private static String dirPath, dirPath2;

final String URL1 = "http://webmaster.com/01/defualt.zip";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dirPath = Utils.getRootDirPath(getApplicationContext());
    dirPath2 = Utils.getRootDirPath(getApplicationContext())+"Unzip";
    init();

    onClickListenerOne();

    Decompress unzip = new Decompress(dirPath+"/download.zip",dirPath2);
    unzip.unzip();

}

解压缩类

public class Decompress {
    private String zip;
    private String loc;

    public Decompress(String zipFile, String location) {
        zip = zipFile;
        loc = location;

        dirChecker("");
    }

    public void unzip() {
        try  {
            FileInputStream fin = new FileInputStream(zip);
            ZipInputStream zin = new ZipInputStream(fin);
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());

                if(ze.isDirectory()) {
                    dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(loc + ze.getName());
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }

                    zin.closeEntry();
                    fout.close();
                }

            }
            zin.close();
        } catch(Exception e) {
            Log.e("Decompress", "unzip", e);
        }

    }

    private void dirChecker(String dir) {
        File f = new File(loc + dir);

        if(!f.isDirectory()) {
            f.mkdirs();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案

您可以使用zip4j提取ZIP文件。

public class Decompress {
    private String zip;
    private String loc;

    public Decompress(String zipFile, String location) {
        zip = zipFile;
        loc = location;

        dirChecker();
    }

    private void dirChecker() {
        File f = new File(loc);

        if(!f.isDirectory()) {
            f.mkdirs();
        }
    }

    public void unzip() {
        try {
            ZipFile zipFile = new ZipFile(zip);
            List<FileHeader> fileHeaders = zipFile.getFileHeaders();
            for (FileHeader fileHeader : fileHeaders) {
                String fileName = fileHeader.getFileName();

                if (fileName.contains("\\")) {
                    fileName = fileName.replace("\\", "\\\\");
                    String[] Folders = fileName.split("\\\\");
                    StringBuilder newFilepath = new StringBuilder();
                    newFilepath.append(loc);
                    for (int i = 0; i < Folders.length - 1; i++) {
                        newFilepath.append(File.separator);
                        newFilepath.append(Folders[i]);
                    }
                    zipFile.extractFile(fileHeader, newFilepath.toString(), null, Folders[Folders.length - 1]);
                } else {
                    zipFile.extractFile(fileHeader, loc);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}