2017-02-24 70 views
2

以下代码来自What is a good Java library to zip/unzip files?的文章Android中ZIP文件夹中的par baseName是什么?

该代码将压缩文件夹以及所有包含的文件和子文件夹转换为zip文件。

我无法理解par BaseName的含义,以及如何将值传递给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(); 
     } 
    } 
} 
+0

'String name = file.getAbsolutePath()。substring(baseName.length());'。这种说法不给你提示吗?请举出名称的例子。我认为你可以使用路径到开始目录。到您要压缩的文件夹。就这些。 – greenapps

+0

谢谢!我将“my”传递给par baseName,但在创建的ZIP文件中找不到字符串“my”! – HelloCW

+0

什么让你觉得你应该通过“我的”?错误。假设你要压缩的文件夹是'/ storage/emulated/0/myfolder',然后将它作为基路径传递。如果你有一个文件'/ storage/emulated/0/myfolder/myfile''name'就会变成'myfile'。 – greenapps

回答

1

我觉得folderAbsolutePath将有可能比baseName一个更好的名字。基本上,如在线String name = file.getAbsolutePath().substring(baseName.length());中可以看到的那样,仅取得相对于文件夹路径的名称。

它需要的原因是因为此方法在递归中,所以需要传递原始文件夹路径以便找出相关文件名。

你可能不希望直接使用此方法,但像其他方法把它包:

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

或更多有用:

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(); 
} 
1

假设是这样的目录结构:

mnt/sdcard/Android/data/testzip/是所有您要归档的文件均为presenet的目录,如下所示,在tobezipped目录下:

mnt/sdcard/Android/data/testzip/tobezipped/1.txt 
mnt/sdcard/Android/data/testzip/tobezipped/directory/2.txt 

如果我们想要创建归档上述所有文件和目录,我们需要存档提供了一个名字,让它成为zipcreated.zip它的目录

mnt/sdcard/Android/data/testzip/下创建作为mnt/sdcard/Android/data/testzip/zipcreated.zip

下面是代码对目录进行归档:

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); 
    } 
    } 
} 
} 
} 

什么是baseName的?

baseName用于支持entryNameZipUtils.addDirToZip()以获取文件或目录名称在此情况下归档。

baseName=/mnt/sdcard/Android/data/testzip/tobezipped: 
entryName=1.txt 
entryName=directory2/2.txt 

基本上,基本名称是什么,但文件名的目录下mnt/sdcard/Android/data/testzip/tobezipped/entryName不能是directory2,因为它是一个目录。

相关问题