Created
August 22, 2019 06:25
-
-
Save nguyenlinhnttu/4de9ad379eae2568509994e8c0d0c8ab to your computer and use it in GitHub Desktop.
ZipManager android code.
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
package com.android.disastersdk.feature; | |
import com.android.disastersdk.utils.Logger; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
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; | |
import java.util.zip.ZipOutputStream; | |
/** | |
* Created by NguyenLinh on 21,August,2019 | |
*/ | |
public class ZipManager { | |
private static int BUFFER_SIZE = 6 * 1024; | |
public static void zip(String[] files, String zipFile) throws IOException { | |
BufferedInputStream origin = null; | |
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); | |
try { | |
byte data[] = new byte[BUFFER_SIZE]; | |
for (int i = 0; i < files.length; i++) { | |
FileInputStream fi = new FileInputStream(files[i]); | |
origin = new BufferedInputStream(fi, BUFFER_SIZE); | |
try { | |
ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); | |
out.putNextEntry(entry); | |
int count; | |
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { | |
out.write(data, 0, count); | |
} | |
} finally { | |
origin.close(); | |
} | |
} | |
} finally { | |
out.close(); | |
} | |
} | |
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[BUFFER_SIZE]; | |
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(); | |
} | |
} | |
//Get all file in folder. | |
File list[] = targetDirectory.listFiles(); | |
for (int i = 0; i < list.length; i++) { | |
Logger.log(list[i].getAbsolutePath()); | |
} | |
} finally { | |
zis.close(); | |
} | |
} | |
} | |
How to use. | |
File resourceFile = new File(context.getExternalFilesDir(null) + File.separator ); | |
try { | |
ZipManager.unzip(new File(filePath), resourceFile); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment