Sunday, March 5, 2017

What is the par baseName in ZIP a Folder in Android?

Leave a Comment

The following code is from the article at What is a good Java library to zip/unzip files?

The code compresses a folder and all included files and sub folders to a zip file.

I can't understand what the par BaseName means, and how to pass a value to the par baseName ?

private void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {     File[] files = folder.listFiles();     for (File file : files) {         if (file.isDirectory()) {             addFolderToZip(file, zip, baseName);         } else {             String name = file.getAbsolutePath().substring(baseName.length());             ZipEntry zipEntry = new ZipEntry(name);             zip.putNextEntry(zipEntry);             IOUtils.copy(new FileInputStream(file), zip);             zip.closeEntry();         }     } } 

2 Answers

Answers 1

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.

Answers 2

I think folderAbsolutePath would have probably be a better name than baseName. Basically, as can seen in the line String name = file.getAbsolutePath().substring(baseName.length());, takes only the name relative to folder path.

The reason it is needed is because this method in recursive, so there's a need to pass the original folder path, in order to figure out relative file names.

You probably don't want to use this method directly, but wrap it with another method like:

private void addFolderToZip(File folder, ZipOutputStream zip){     addFolderToZip(folder, zip, folder.getAbsolutePath()); } 

or more useful:

public void zip(String sourceFileName, String zipName){     File file = new File(source);      FileOutputStream fileStream = new FileOutputStream(zipName);     BufferedOutputStream bufferdStream = new BufferedOutputStream(fileStream);     ZipOutputStream zip = new ZipOutputStream(bufferdStream);      if (sourceFile.isFolder){         addFolderToZip(file, ZipOutputStream zip, sourceFile.getAbsolutePath());     } else {         // some other handling for a single file         int filePathLength = file.getAbsolutePath().length();         int fileNameLength = file.getName().length();         String basePath = file.getAbsolutePath().substring(0, filePathLength - fileNameLength);          addFolderToZip(file, ZipOutputStream zip, basePath);     }     zip.close();  } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment