您好我正在尝试解压缩文件时使用以下代码段
public static List<String> unZip(File tarFile) throws IOException {
List<String> result = new ArrayList<String>();
InputStream inputStream = new FileInputStream(tarFile);
ZipArchiveInputStream in = new ZipArchiveInputStream(inputStream);
ZipArchiveEntry entry = in.getNextZipEntry();
while (entry != null) {
System.out.println("Entry name ::: "+entry);
if (entry.isDirectory()) {
System.out.println("This entry is a directory ");
entry = in.getNextZipEntry();
continue;
}
result.add(entry.getName());
entry = in.getNextZipEntry();
}
in.close();
return result;
}
我上面的代码段输出为
Entry name ::: lib/docssdk.jar
Entry name ::: lib/jackson-annotations-2.7.4.jar
Entry name ::: lib/jackson-core-2.7.4.jar
Entry name ::: lib/jackson-databind-2.7.4.jar
Entry name ::: lib/jsonapi-converter-0.3.jar
Entry name ::: lib/okhttp-3.0.1.jar
Entry name ::: lib/okio-1.7.0.jar
Entry name ::: lib/retrofit-2.0.2.jar
我期待它成为
Entry name ::: lib/
Entry name ::: lib/docssdk.jar
Entry name ::: lib/jackson-annotations-2.7.4.jar
Entry name ::: lib/jackson-core-2.7.4.jar
Entry name ::: lib/jackson-databind-2.7.4.jar
Entry name ::: lib/jsonapi-converter-0.3.jar
Entry name ::: lib/okhttp-3.0.1.jar
Entry name ::: lib/okio-1.7.0.jar
Entry name ::: lib/retrofit-2.0.2.jar
根文件夹lib /缺失
当我在我的机器中解压缩文件内容并在lib文件夹上再次创建一个zip时,我得到了预期的输出。
为什么我在获取getNextZipEntry
时错过了我的根文件夹?请就此向我提出建议。
我附上了screenshot的zip结构以及
Original Zip File重现此问题
答案 0 :(得分:4)
您没有看到lib/
条目,因为它在您的zip文件中不存在:
$ zipinfo zip_root_folder_issue.zip
Archive: zip_root_folder_issue.zip
Zip file size: 1878250 bytes, number of entries: 8
-rw-r--r-- 3.0 unx 17596 bx defN 16-Aug-02 13:38 lib/docssdk.jar
-rw-r--r-- 3.0 unx 50897 bx defN 16-Aug-02 13:40 lib/jackson-annotations-2.7.4.jar
-rw-r--r-- 3.0 unx 253001 bx defN 16-Aug-02 13:39 lib/jackson-core-2.7.4.jar
-rw-r--r-- 3.0 unx 1204187 bx defN 16-Aug-02 13:39 lib/jackson-databind-2.7.4.jar
-rw-r--r-- 3.0 unx 36004 bx defN 16-Aug-02 13:39 lib/jsonapi-converter-0.3.jar
-rw-r--r-- 3.0 unx 326467 bx defN 16-Aug-02 13:39 lib/okhttp-3.0.1.jar
-rw-r--r-- 3.0 unx 69868 bx defN 16-Aug-02 13:38 lib/okio-1.7.0.jar
-rw-r--r-- 3.0 unx 86225 bx defN 16-Aug-02 13:38 lib/retrofit-2.0.2.jar
8 files, 2044245 bytes uncompressed, 1876818 bytes compressed: 8.2%
档案管理员可能会显示中间“缺失”文件夹,但zip文件不需要输入。使用zip(例如xlsx)的某些文件格式甚至不希望它们都没有。
在您的情况下,我认为您需要执行这些管理员所做的事情:显示/创建文件夹(如果该文件夹不存在)。
答案 1 :(得分:2)
@subramanian rasapan,如果您只想提取Zip文件,那么我可以建议第三方工具进行最简单的提取,并解决上述问题。
该工具的名称是Zip4J:www.lingala.net/zip4j/download.php
只需将其添加到您的库中,您就可以使用以下代码进行提取:
public void unZipFile(String filePath)
{
String destination = "<desired-destination>";
try
{
String zipFileName = new File(filePath).getName();
ZipFile zipFile = new ZipFile(filePath);
zipFile.extractAll(destination);
}
catch (ZipException e)
{
}
}
要使上述代码生效,您需要以下导入:
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
我正在提供一个测试程序来解压缩任何zip文件:
package unzip;
import java.io.File;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class Unzip
{
public static void main(String[] args)
{
String fileName = "abc.zip";
String destination = "D:\\copiedTo";
try
{
String zipFileName = new File(fileName).getName();
ZipFile zipFile = new ZipFile(fileName);
zipFile.extractAll(destination+"\\"+zipFileName);
}
catch (ZipException e)
{
}
}
}
Zip文件中提取的文件将保存到d:\ copiedto \\。
我的拉链结构:
abc.zip (zip file) |_abc |_1.txt |_2.txt |_3.txt |_4.txt |_5.txt |_6.txt
提取时,文件夹与zip文件的结构相同(连同根文件夹)。
我正在附上我测试过的文件的链接,如有必要请检查它们:
Zip文件:https://drive.google.com/file/d/0B4fTzD5zwqL0M1lqM3EwazVzWUE/view?usp=sharing 提取的文件:https://drive.google.com/open?id=0B4fTzD5zwqL0T0xVaXhmSzZ6VWc
请检查并确认是否有任何混淆。 谢谢和问候。