Suppose this is the directory structure:
mnt/sdcard/Android/data/testzip/
is the directory where all your files to be archived are presenet as follows under tobezipped
directory:
mnt/sdcard/Android/data/testzip/tobezipped/1.txt
mnt/sdcard/Android/data/testzip/tobezipped/directory/2.txt
If we want to create archive all of the above files and directory, we need to provide a name for the archive, and let it be zipcreated.zip which is created under the directory
mnt/sdcard/Android/data/testzip/
as mnt/sdcard/Android/data/testzip/zipcreated.zip
Below is the code for the directory to be archived:
new Thread(new Runnable() {
@Override
public void run() {
// Moves the current Thread into the background
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
//tobezipped holds the content of the directory or folder to be zipped
//zipcreated.zip zips or archive holds the contents of the tobezipped after archived
ZipUtils.createZipFileOfDirectory("mnt/sdcard/Android/data/testzip/tobezipped/",
"mnt/sdcard/Android/data/testzip/zipcreated.zip");
}
}).start();
==========================================================================
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;
import org.apache.commons.io.IOUtils;
/**
* Created by Android Studio
* User: Anurag Singh
* Date: 28/2/17
* Time: 2:14 PM
*/
public class ZipUtils {
private static final String TAG = ZipUtils.class.getSimpleName();
public static void createZipFileOfDirectory(String srcDir, String zipOutput) {
try{
File srcDirFile = new File(srcDir);
File zipOutputFile = new File(zipOutput);
if ( !srcDirFile.exists() || !srcDirFile.isDirectory() ) {
throw new IllegalArgumentException(
srcDirFile.getAbsolutePath() + " is not a directory!");
}
if ( zipOutputFile.exists() && !zipOutputFile.isFile() ) {
throw new IllegalArgumentException(
zipOutputFile.getAbsolutePath() + " exists but is not a file!");
}
ZipOutputStream zipOutputStream = null;
String baseName = srcDirFile.getAbsolutePath() + File.pathSeparator;
try {
zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutput));
addDirToZip(srcDirFile, zipOutputStream, baseName);
} finally {
IOUtils.close(zipOutputStream);
}
}catch(Exception e){
e.printStackTrace();
}
}
private static void addDirToZip(File dir, ZipOutputStream zip, String baseName) throws IOException {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
addDirToZip(file, zip, baseName);
} else {
String entryName = file.getAbsolutePath().substring(
baseName.length());
ZipEntry zipEntry = new ZipEntry(entryName);
zip.putNextEntry(zipEntry);
FileInputStream fileInput = new FileInputStream(file);
try {
IOUtils.copy(fileInput, zip);
zip.closeEntry();
} finally {
IOUtils.close(fileInput);
}
}
}
}
}
What is baseName?
baseName
is used as a support for entryName
in ZipUtils.addDirToZip()
to get the file or directory name to be archived from in this case.
baseName=/mnt/sdcard/Android/data/testzip/tobezipped:
entryName=1.txt
entryName=directory2/2.txt
Basically, base name is nothing but the file names under directory mnt/sdcard/Android/data/testzip/tobezipped/
. entryName
cannot be directory2
becasue it's a directory.