我需要解包所有包含zip文件的文件。但是zip文件中还包含一个.tar文件。考虑以下路径:原始zip路径:e:\test\26-03-2016\order\7930199\u 1.zip提取路径:e:\test\26-03-2016\order\7930199\u 1\itemfile\1458887416277\u 12\ftp\content providers\ewh-e\data\incomingabp
In the Extracted path ,The folder contains a .tar file as OBX00000000005442A.tar and it has several folders with in it.
我已提取到incomingabp,但无法提取.tar文件。请帮助我提取这个.tar文件。下面我提供了我的代码片段。
代码从这里开始
static public void extractFolder(String zipFile) throws
ZipException,
IOException
{
System.out.println(zipFile);
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.length() - 4);
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
if (currentEntry.endsWith(".zip")||currentEntry.endsWith(".tar"))
{
// found a zip file, try to open
extractFolder(destFile.getAbsolutePath());
}
}
}
}
有人能帮我提取这个吗?提前感谢strack trace:e:\test\26-03-2016\order\7930199\u 1.zip e:\test\26-03-2016\order\7930199\u 1\itemfile\1458887416277\u 12.zip e:\test\26-03-2016\order\7930199\u 1\itemfile\1458887416277\u 12\ftp\content-providers\ewh-e\data\incomingabp\obx0000005442a.tar
Exception in thread "main" java.util.zip.ZipException: error in opening
zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:24)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:68)
at Extraction_ZIP.Extraction3.extractFolder(Extraction3.java:68)
at Execution_point.Cl_Execute.main(Cl_Execute.java:29)
1条答案
按热度按时间cx6n0qe31#
这个问题在这里已经回答过了。如果问题已经存在,最好还是搜索。
无论如何,这里有一个非常相似的问题。似乎使用ApacheCommonsCompress是一个不错的选择。
如何在java中提取tar文件?