1

I want to make a project to Download file And extract files. I tried fix it so long time. Please looking my Code and help me please, Or someone tell me how to download file and extrack zip file. In file "download.zip" contains 5 video files. I use Class Decompress from 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();

}

Decompress Class

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();
        }
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Solution

You can use zip4j to extract ZIP file.

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();
        }
    }
}
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62