Java 解压包含和不包含子目录的多部分文件
解压多部分文件带子目录和不带子目录,因为我没有给用户任何关于如何解压文件的指示,因此我需要从zip文件中查找/搜索所有文件,这些文件可能有目录和子目录,并将所有文件保持在不同的中文件夹 所以基本上它是一种智能解压,使用ZipEntry检测目录,然后跳过并找到要写入文件夹的文件 我写了一个代码,但我甚至不接近它,因为我得到的只有一个文件,其中也没有目录Java 解压包含和不包含子目录的多部分文件,java,unzip,multipart,Java,Unzip,Multipart,解压多部分文件带子目录和不带子目录,因为我没有给用户任何关于如何解压文件的指示,因此我需要从zip文件中查找/搜索所有文件,这些文件可能有目录和子目录,并将所有文件保持在不同的中文件夹 所以基本上它是一种智能解压,使用ZipEntry检测目录,然后跳过并找到要写入文件夹的文件 我写了一个代码,但我甚至不接近它,因为我得到的只有一个文件,其中也没有目录 String outputPath="C:\\Users\\Plootus\\exceldocs\\"; FileSystem fileS
String outputPath="C:\\Users\\Plootus\\exceldocs\\";
FileSystem fileSystem = FileSystems.getDefault();
try
{
ZipInputStream zis=new ZipInputStream(serverFile.getInputStream());
BufferedInputStream bis=null;
InputStream is=null;
//Get file entries
ZipEntry entry=null;
//We will unzip files in this folder
while ( (entry = zis.getNextEntry()) != null ) {
System.out.println(entry.getName());
if(!entry.isDirectory()) {
System.out.println(entry.getName());
is = zis;
bis = new BufferedInputStream(is);
String uncompressedFileName = outputPath+toolName+entry.getName();
Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
Files.createFile(uncompressedFilePath);
FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
while (bis.available() > 0)
{
fileOutput.write(bis.read());
}
fileOutput.close();
System.out.println("Written :" + entry.getName());
bis.close();
is.close();
}
}
zis.close();
return true;
}
catch(IOException e)
{
return false;
}
return false;
目标:Zip文件包含可能的条目
1.)abc.zip(多部分文件)
而不是从多部分文件中提取并将条目作为ZipEntry处理(正如@Jokkeri所说的),这是不可能的,因此我找到了其他方法 我将保存该文件,操作完成后将其删除 收到多部分文件后,我使用文件对象(saveZip)
try(ZipFile file = new ZipFile(saveZip.getCanonicalPath()))
{
FileSystem fileSystem = FileSystems.getDefault();
//Get file entries
Path inputpath=fileSystem.getPath(file.getName());
Enumeration<? extends ZipEntry> entries = file.entries();
//We will unzip files in this folder
File directory=new File(zipFilePath.concat(username+"-"+toolName));
if(!directory.exists()) {
directory.mkdir();
}
//Iterate over entries
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
String abc[]=entry.getName().split("/");
//Else create the file
if(!entry.isDirectory())
{
InputStream is = file.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
String uncompressedFileName = zipFilePath +username+"-"+toolName+"/"+ abc[abc.length-1];
Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
if(Files.notExists(uncompressedFilePath))
Files.createFile(uncompressedFilePath);
FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
while (bis.available() > 0)
{
fileOutput.write(bis.read());
}
fileOutput.close();
System.out.println("Written :" + entry.getName());
is.close();
bis.close();
}
}
file.close();
Files.deleteIfExists(inputpath);
return true;
}catch(IOException e)
{
e.printStackTrace();
return false;
}
try(ZipFile文件=新的ZipFile(saveZip.getCanonicalPath())
{
FileSystem FileSystem=FileSystems.getDefault();
//获取文件条目
Path inputpath=fileSystem.getPath(file.getName());
枚举我不完全确定我是否理解。您的用户上载了一个包含多个文件的zip文件?(您的意思是“多部分”吗?)并且您希望忽略用户在.zip中提供的目录结构并创建自己的目录结构?并且您希望将每个提取的文件放在一个单独的文件夹中?(以防名称冲突?)你说的是单独的目录,但我在你的代码中看不到。我根本看不到你在创建任何目录-ZipEntry.getName()是返回完整路径还是只返回最终名称组件?如果是完整路径,如果Files.createFile()将自动为您在路径中创建丢失的目录。@Rup否,我希望所有文件都意味着任何文件而不是文件夹都被解压缩并保存在单独的文件夹中,其中没有层次结构。我将再次更新问题以显示可能的结构。还有Files.createFile()将在目录中创建文件,因为我在其中传递了路径,该路径包含outpath+entry.getname();和yes entry.getname()仅获取您的文件名我猜您的意思是,无论ZIP文件是否包含文件夹结构,都要将所有文件解压缩到单个文件夹中吗?@Plootus不确定这是否有意义,因为您从其中获取inputstream并将条目作为ZipEntry处理。。。
-folder1--bio.csv(file)-folder-2(inside folder1)-arkan.csv,dan.csv,kud.csv
-arkan.csv,dan.csv,kud.csv
try(ZipFile file = new ZipFile(saveZip.getCanonicalPath()))
{
FileSystem fileSystem = FileSystems.getDefault();
//Get file entries
Path inputpath=fileSystem.getPath(file.getName());
Enumeration<? extends ZipEntry> entries = file.entries();
//We will unzip files in this folder
File directory=new File(zipFilePath.concat(username+"-"+toolName));
if(!directory.exists()) {
directory.mkdir();
}
//Iterate over entries
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
String abc[]=entry.getName().split("/");
//Else create the file
if(!entry.isDirectory())
{
InputStream is = file.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
String uncompressedFileName = zipFilePath +username+"-"+toolName+"/"+ abc[abc.length-1];
Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
if(Files.notExists(uncompressedFilePath))
Files.createFile(uncompressedFilePath);
FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName);
while (bis.available() > 0)
{
fileOutput.write(bis.read());
}
fileOutput.close();
System.out.println("Written :" + entry.getName());
is.close();
bis.close();
}
}
file.close();
Files.deleteIfExists(inputpath);
return true;
}catch(IOException e)
{
e.printStackTrace();
return false;
}