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

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


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();

        }

    }

}


有只小跳蛙
浏览 173回答 1
1回答

森林海

解决方案您可以使用zip4j来提取 ZIP 文件。public class Decompress {&nbsp; &nbsp; private String zip;&nbsp; &nbsp; private String loc;&nbsp; &nbsp; public Decompress(String zipFile, String location) {&nbsp; &nbsp; &nbsp; &nbsp; zip = zipFile;&nbsp; &nbsp; &nbsp; &nbsp; loc = location;&nbsp; &nbsp; &nbsp; &nbsp; dirChecker();&nbsp; &nbsp; }&nbsp; &nbsp; private void dirChecker() {&nbsp; &nbsp; &nbsp; &nbsp; File f = new File(loc);&nbsp; &nbsp; &nbsp; &nbsp; if(!f.isDirectory()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.mkdirs();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void unzip() {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZipFile zipFile = new ZipFile(zip);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<FileHeader> fileHeaders = zipFile.getFileHeaders();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (FileHeader fileHeader : fileHeaders) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fileName = fileHeader.getFileName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fileName.contains("\\")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileName = fileName.replace("\\", "\\\\");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] Folders = fileName.split("\\\\");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder newFilepath = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newFilepath.append(loc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < Folders.length - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newFilepath.append(File.separator);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newFilepath.append(Folders[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipFile.extractFile(fileHeader, newFilepath.toString(), null, Folders[Folders.length - 1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipFile.extractFile(fileHeader, loc);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java