Guest User

Untitled

a guest
Aug 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. Unzip a zipped file on sd card in Android application
  2. import android.util.Log;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipInputStream;
  8.  
  9. /**
  10. *
  11. * @author jon
  12. */
  13. public class Decompress {
  14. private String _zipFile;
  15. private String _location;
  16.  
  17. public Decompress(String zipFile, String location) {
  18. _zipFile = zipFile;
  19. _location = location;
  20.  
  21. _dirChecker("");
  22. }
  23.  
  24. public void unzip() {
  25. try {
  26. FileInputStream fin = new FileInputStream(_zipFile);
  27. ZipInputStream zin = new ZipInputStream(fin);
  28. ZipEntry ze = null;
  29. while ((ze = zin.getNextEntry()) != null) {
  30. Log.v("Decompress", "Unzipping " + ze.getName());
  31.  
  32. if(ze.isDirectory()) {
  33. _dirChecker(ze.getName());
  34. } else {
  35. FileOutputStream fout = new FileOutputStream(_location + ze.getName());
  36. for (int c = zin.read(); c != -1; c = zin.read()) {
  37. fout.write(c);
  38. }
  39.  
  40. zin.closeEntry();
  41. fout.close();
  42. }
  43.  
  44. }
  45. zin.close();
  46. } catch(Exception e) {
  47. Log.e("Decompress", "unzip", e);
  48. }
  49.  
  50. }
  51.  
  52. private void _dirChecker(String dir) {
  53. File f = new File(_location + dir);
  54.  
  55. if(!f.isDirectory()) {
  56. f.mkdirs();
  57. }
  58. }
  59. }
  60.  
  61. String zipFilename = Environment.getExternalStorageDirectory() + "/files.zip";
  62. String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/";
  63.  
  64. Decompress d = new Decompress(zipFilename, unzipLocation);
  65. d.unzip();
Add Comment
Please, Sign In to add comment