Last active
February 23, 2019 23:39
-
-
Save FootballFan141/b1db1924f13b167d1fd127db6a764cb9 to your computer and use it in GitHub Desktop.
Using this, when decompressing a file around 800mb on my nexus 7, the app slows down badly. *SOLUTION: Use asynctask*
This file contains hidden or 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
//Called on a button click in my Main activity file | |
public MainActivity instance; | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
instance = getInstance(); | |
//rest of code... | |
} | |
public MainActivity getInstance() { | |
if (instance == null) { | |
instance = this; | |
} | |
return instance; | |
} | |
try { | |
new ZipTool(instance).decompress(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} |
This file contains hidden or 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
import android.util.Log; | |
import com.appsbytravis.acmusic.MainActivity; | |
import com.snatik.storage.Storage; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Locale; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
public class ZipTool { | |
private final MainActivity instance; | |
private int prepareAssetsProgress = 0; | |
private static final int BUFFER_SIZE = 1024; | |
private Storage storage; | |
private String path; | |
private static final String ASSET_FILE = "/music_assets.zip"; | |
private static final String ASSETS_PATH = "/assets/"; | |
public ZipTool(MainActivity instance) { | |
this.instance = instance; | |
storage = new Storage(instance); | |
path = storage.getInternalFilesDirectory(); | |
} | |
public void decompress() throws IOException { | |
File file = storage.getFile(path.concat(ASSET_FILE)); | |
FileInputStream fis = new FileInputStream(file.getPath()); | |
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); | |
ZipEntry entry; | |
byte[] bytesIn = new byte[BUFFER_SIZE]; | |
while ((entry = zis.getNextEntry()) != null) { | |
if (instance.isPreparing) { | |
String filePath = path.concat(ASSETS_PATH).concat(entry.getName()); | |
long size = entry.getSize(); | |
if (!entry.isDirectory()) { | |
Log.d(instance.TAG, entry.getName()); | |
FileOutputStream fos = new FileOutputStream(filePath); | |
BufferedOutputStream bos = new BufferedOutputStream(fos); | |
int read; | |
while ((read = zis.read(bytesIn)) != -1) { | |
bos.write(bytesIn, 0, read); | |
prepareAssetsProgress += bytesIn.length; | |
instance.bar.setProgress((int) (prepareAssetsProgress / size)); | |
instance.progressTextView.setText(String.format(Locale.US, "%d%%", (int) (prepareAssetsProgress / size))); | |
} | |
fos.close(); | |
} else { | |
Log.d(instance.TAG, entry.getName()); | |
storage.createDirectory(filePath); | |
} | |
zis.closeEntry(); | |
} else { | |
break; | |
} | |
} | |
zis.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment