Last active
August 29, 2015 14:18
[Android] android zip file make
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 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.ZipOutputStream; | |
public class HZipUtil { | |
byte[] buffer = new byte[4096]; | |
public void zip(String targetPath, String returnPath) { | |
try { | |
File targetFile = new File(targetPath); | |
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(returnPath))); | |
if (targetFile.isDirectory()) { | |
File[] file = targetFile.listFiles(); | |
for (int i = 0; i < file.length; i++) { | |
addZipFile(zos, file[i]); | |
} | |
} else { | |
addZipFile(zos, targetFile); | |
} | |
zos.closeEntry(); | |
// remember close it | |
zos.close(); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private void addZipFile(ZipOutputStream zos, File file) throws IOException { | |
int len; | |
zos.putNextEntry(new ZipEntry(file.getName())); | |
FileInputStream fis = new FileInputStream(file.getAbsolutePath()); | |
while ((len = fis.read(buffer)) > 0) { | |
zos.write(buffer, 0, len); | |
} | |
fis.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment