Created
February 2, 2016 16:20
-
-
Save mirjalal/848093a07437defb14bc to your computer and use it in GitHub Desktop.
Java class for zipping files and folders [working]
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
import android.util.Log; | |
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.zip.ZipEntry; | |
import java.util.zip.ZipOutputStream; | |
public class Zip { | |
/** | |
* @param sourcePath | |
* folder to zip | |
* @param toLocation | |
* destination to copying zip file | |
* @return | |
* true if zipping process finish successfully | |
*/ | |
public boolean zipFileAtPath(String sourcePath, String toLocation) { | |
final int BUFFER = 2048; | |
File sourceFile = new File(sourcePath); | |
try { | |
BufferedInputStream origin; | |
FileOutputStream dest = new FileOutputStream(toLocation); | |
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); | |
if (sourceFile.isDirectory()) | |
zipSubFolder(out, sourceFile, sourceFile.getParent().length()); | |
else { | |
byte data[] = new byte[BUFFER]; | |
FileInputStream fi = new FileInputStream(sourcePath); | |
origin = new BufferedInputStream(fi, BUFFER); | |
ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath)); | |
out.putNextEntry(entry); | |
int count; | |
while ((count = origin.read(data, 0, BUFFER)) != -1) | |
out.write(data, 0, count); | |
} | |
out.close(); | |
return true; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
/* | |
* Zips a subfolder | |
*/ | |
private void zipSubFolder(ZipOutputStream out, File folder, int basePathLength) throws IOException { | |
final int BUFFER = 2048; | |
File[] fileList = folder.listFiles(); | |
BufferedInputStream origin; | |
for (File file : fileList) { | |
if (file.isDirectory()) { | |
zipSubFolder(out, file, basePathLength); | |
} else { | |
byte data[] = new byte[BUFFER]; | |
String unmodifiedFilePath = file.getPath(); | |
String relativePath = unmodifiedFilePath | |
.substring(basePathLength); | |
Log.i("ZIP SUBFOLDER", "Relative Path : " + relativePath); | |
FileInputStream fi = new FileInputStream(unmodifiedFilePath); | |
origin = new BufferedInputStream(fi, BUFFER); | |
ZipEntry entry = new ZipEntry(relativePath); | |
out.putNextEntry(entry); | |
int count; | |
while ((count = origin.read(data, 0, BUFFER)) != -1) | |
out.write(data, 0, count); | |
origin.close(); | |
} | |
} | |
} | |
/** | |
* @param filePath | |
* string (source) path | |
* @return | |
* last component of filePath | |
*/ | |
private String getLastPathComponent(String filePath) { | |
String[] segments = filePath.split("/"); | |
return segments[segments.length - 1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment