假设是这样的目录结构:
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
用于支持entryName
的ZipUtils.addDirToZip()
以获取文件或目录名称在此情况下归档。
baseName=/mnt/sdcard/Android/data/testzip/tobezipped:
entryName=1.txt
entryName=directory2/2.txt
基本上,基本名称是什么,但文件名的目录下mnt/sdcard/Android/data/testzip/tobezipped/
。 entryName
不能是directory2
,因为它是一个目录。
'String name = file.getAbsolutePath()。substring(baseName.length());'。这种说法不给你提示吗?请举出名称的例子。我认为你可以使用路径到开始目录。到您要压缩的文件夹。就这些。 – greenapps
谢谢!我将“my”传递给par baseName,但在创建的ZIP文件中找不到字符串“my”! – HelloCW
什么让你觉得你应该通过“我的”?错误。假设你要压缩的文件夹是'/ storage/emulated/0/myfolder',然后将它作为基路径传递。如果你有一个文件'/ storage/emulated/0/myfolder/myfile''name'就会变成'myfile'。 – greenapps