Last active
June 23, 2018 09:06
-
-
Save mkrupal09/7192080976ec58436c7573c49a8deec9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dc.myapplication; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v7.app.AppCompatActivity; | |
import java.io.BufferedInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
public class MainActivity extends AppCompatActivity { | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
File obbDir = getObbDir(); | |
File fileToExtract = new File(obbDir, "main.1.com.dc.android.zip"); | |
File extractTo = new File(Environment.getExternalStorageDirectory(), "Extracted"); | |
if (!extractTo.exists()) extractTo.mkdir(); | |
try { | |
unzip(fileToExtract, extractTo); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void unzip(File zipFile, File targetDirectory) throws IOException { | |
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile))); | |
try { | |
ZipEntry ze; | |
int count; | |
byte[] buffer = new byte[8192]; | |
while ((ze = zis.getNextEntry()) != null) { | |
File file = new File(targetDirectory, ze.getName()); | |
File dir = ze.isDirectory() ? file : file.getParentFile(); | |
if (!dir.isDirectory() && !dir.mkdirs()) { | |
throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath()); | |
} | |
if (ze.isDirectory()) continue; | |
FileOutputStream fout = new FileOutputStream(file); | |
try { | |
while ((count = zis.read(buffer)) != -1) fout.write(buffer, 0, count); | |
} finally { | |
fout.close(); | |
} | |
/* if time should be restored as well | |
long time = ze.getTime(); | |
if (time > 0) | |
file.setLastModified(time); | |
*/ | |
} | |
} finally { | |
zis.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment