需要解压缩文件夹中的所有.zip格式 [英] need to unzip all .zip format in the folder
本文介绍了需要解压缩文件夹中的所有.zip格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过这个,但它只能解压缩一个文件,我必须提供确切的zip文件名
但我需要做的是我需要解压缩文件夹中的所有zip文件只给出文件夹路径
package unziptry;
/ * *
*
* @author Gayan
* /
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class DeCompressZipFileExample {
public static void main( String [] args) throws 异常{
String zipFile = C:\\Users \\Gayan \\Desktop \\ gayan \\ \\\New_folder.zip跨度>;
字符串 outputFolder = C:\ \Users\\Gayan\\Desktop\\gayan跨度>;
System.out.println( 开始解压缩 + zipFile + < span class =code-string> into + outputFolder);
ZipInputStream zis = new ZipInputStream( new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while (ze!= null){
String entryName = ze.getName ();
System.out.print( 提取 + entryName + - > + outputFolder + File.separator + entryName + ...);
文件f = 新文件(outputFolder + File.separator + entryName);
// 创建以正确的相对路径存储所需的所有文件夹。
f .getParentFile()mkdirs();
FileOutputStream fos = new FileOutputStream(f);
int len;
byte buffer [] = new byte [ 1024 ];
while ((len = zis.read(buffer))> 0 ){
fos.write(buffer, 0 ,len);
}
fos.close();
System.out.println( OK!);
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println(zipFile + 成功解压缩);
}
}
解决方案
你好Gayan,
下面是一个非常简单的代码片段,展示了如何从给定路径中检索zip文件。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnZipFiles {
private static final Logger _LOGGER = LoggerFactory.getLogger(UnZipFiles。类跨度>);
public void unzipAll(字符串路径){
字符串 filName;
文件夹= 新文件(路径);
File [] listOfFiles = folder.listFiles();
for ( int i = 0 ; i< listOfFiles.length; i ++){
if (listOfFiles [i] .isFile()){
filName = listOfFiles [I] .getName();
if (files.endsWith( )。 zip)|| files.endsWith( .ZIP)){
unzipFile(listOfFiles [i]);
}
}
}
}
public void unZipFile(File zipFile){
byte [] buffer = new byte [ 1024 ];
尝试 {
// 创建输出目录不存在
文件夹= 新文件(OUTPUT_FOLDER);
if (!folder.exists()){
folder.mkdir();
}
// 获取zip文件内容
ZipInputStream zis = new ZipInputStream( new FileInputStream(zipFile));
// 获取压缩文件列表条目
ZipEntry ze = zis.getNextEntry ();
while (ze!= null){
String fileName = ze.getName();
文件newFile = 新文件(outputFolder + File.separator + fileName);
if (_LOGGER.isDebugEnabled())
_LOGGER.debug( 解压缩文件:{}, new Object [] {newFile.getAbsoluteFile()}) ;
// 创建所有不存在的文件夹
// 否则您将针对压缩文件夹点击FileNotFoundException
new 文件(newFile.getParent())。mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer))> 0 ){
fos.write(buffer, 0 ,len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (IOException ex){
_LOGGER.error( 解压缩时发生异常!,ex);
}
}
}
希望有所帮助。
的问候,
i tried this but it can only unzip one file and i have to give the exact zip file name
but what i need do is i need to unzip all zip files in a folder only giving the folder path
package unziptry;
/**
*
* @author Gayan
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class DeCompressZipFileExample {
public static void main(String[] args) throws Exception {
String zipFile = "C:\\Users\\Gayan\\Desktop\\gayan\\New_folder.zip";
String outputFolder = "C:\\Users\\Gayan\\Desktop\\gayan";
System.out.println("Begin unzip " + zipFile + " into " + outputFolder);
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String entryName = ze.getName();
System.out.print("Extracting " + entryName + " -> " + outputFolder + File.separator + entryName + "...");
File f = new File(outputFolder + File.separator + entryName);
//create all folder needed to store in correct relative path.
f.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(f);
int len;
byte buffer[] = new byte[1024];
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
System.out.println("OK!");
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println(zipFile + " unzipped successfully");
}
}
解决方案
Hello Gayan,
Below a very simple code snippet shows how to retrieve zip files from the given path.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UnZipFiles { private static final Logger _LOGGER = LoggerFactory.getLogger(UnZipFiles.class); public void unzipAll(String path) { String filName; File folder = new File(path); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { filName = listOfFiles[i].getName(); if (files.endsWith(".zip") || files.endsWith(".ZIP")) { unzipFile(listOfFiles[i]); } } } } public void unZipFile(File zipFile) { byte[] buffer = new byte[1024]; try { // create output directory is not exists File folder = new File(OUTPUT_FOLDER); if (!folder.exists()) { folder.mkdir(); } // get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); // get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); if (_LOGGER.isDebugEnabled()) _LOGGER.debug("Unzipping file : {}", new Object[] {newFile.getAbsoluteFile()}); // create all non exists folders // else you will hit FileNotFoundException for compressed folder 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(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch(IOException ex) { _LOGGER.error("Exception occurred while unzipping!", ex); } } }
Hope it helps.
Regards,
这篇关于需要解压缩文件夹中的所有.zip格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文