带和不带子目录的多部分文件解压缩

时间:2018-08-13 09:42:59

标签: java unzip multipart

解压缩多部分文件 带子目录和不带子目录,因为我没有向用户提供任何有关如何压缩文件的说明,因此我需要 查找/ search 中的所有Zip文件中可能包含目录和子目录的文件,并将所有文件保存在单独的其他文件夹中。

所以基本上,这是一种智能解压缩,它使用ZipEntry检测目录,然后跳过并找到要写入文件夹的文件。

我已经写了一个代码,但是我什至没有,因为我也只得到一个没有目录的文件。

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(多部分文件)

    -folder1-arkan.csv,dan.csv,kud.csv
  1. abc.zip(Mutlipart文件)

    -folder1--bio.csv(file)-folder-2(inside folder1)-arkan.csv,dan.csv,kud.csv
    
  2. abc.zip(Mutlipart文件)

    -arkan.csv,dan.csv,kud.csv
    

1 个答案:

答案 0 :(得分:1)

不可能从MultipartFile中提取并像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;
        }