java 上文件传示例
Welcome to Java Unzip File Example. In the last post, we learned how to zip file and directory in java, here we will unzip the same zip file created from directory to another output directory.
欢迎使用Java解压缩文件示例。 在上一篇文章中,我们学习了如何在java中压缩文件和目录 ,这里我们将从目录创建的相同zip文件解压缩到另一个输出目录。
Java解压缩文件 (Java Unzip File)
To unzip a zip file, we need to read the zip file with ZipInputStream
and then read all the ZipEntry
one by one. Then use FileOutputStream
to write them to file system.
要解压缩一个zip文件,我们需要使用ZipInputStream
读取该zip文件,然后逐个读取所有ZipEntry
。 然后使用FileOutputStream
将它们写入文件系统。
We also need to create the output directory if it doesn’t exists and any nested directories present in the zip file.
如果输出目录不存在,并且zip文件中存在任何嵌套目录,我们还需要创建输出目录。
Here is the java unzip file program that unzips the earlier created tmp.zip
to output directory.
这是一个Java解压缩文件程序,它将先前创建的tmp.zip
解压缩到输出目录。
package com.journaldev.files;
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.ZipInputStream;
public class UnzipFiles {
public static void main(String[] args) {
String zipFilePath = "/Users/pankaj/tmp.zip";
String destDir = "/Users/pankaj/output";
unzip(zipFilePath, destDir);
}
private static void unzip(String zipFilePath, String destDir) {
File dir = new File(destDir);
// create output directory if it doesn't exist
if(!dir.exists()) dir.mkdirs();
FileInputStream fis;
//buffer for read and write data to file
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = zis.getNextEntry();
while(ze != null){
String fileName = ze.getName();
File newFile = new File(destDir + File.separator + fileName);
System.out.println("Unzipping to "+newFile.getAbsolutePath());
//create directories for sub directories in zip
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
//close this ZipEntry
zis.closeEntry();
ze = zis.getNextEntry();
}
//close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Once the program finishes, we have all the zip file contents in the output folder with same directory hierarchy.
程序完成后,我们将所有zip文件内容存储在具有相同目录层次结构的输出文件夹中。
Output of the above program is:
上面程序的输出是:
Unzipping to /Users/pankaj/output/.DS_Store
Unzipping to /Users/pankaj/output/data/data.dat
Unzipping to /Users/pankaj/output/data/data.xml
Unzipping to /Users/pankaj/output/data/xmls/project.xml
Unzipping to /Users/pankaj/output/data/xmls/web.xml
Unzipping to /Users/pankaj/output/data.Xml
Unzipping to /Users/pankaj/output/DB.xml
Unzipping to /Users/pankaj/output/item.XML
Unzipping to /Users/pankaj/output/item.xsd
Unzipping to /Users/pankaj/output/ms/data.txt
Unzipping to /Users/pankaj/output/ms/project.doc
That’s all about java unzip file example.
这就是有关Java解压缩文件示例的全部内容。
java 上文件传示例