Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用spring boot将zip文件从应用程序资源文件夹复制到目标_Java_Spring_Spring Boot - Fatal编程技术网

Java 使用spring boot将zip文件从应用程序资源文件夹复制到目标

Java 使用spring boot将zip文件从应用程序资源文件夹复制到目标,java,spring,spring-boot,Java,Spring,Spring Boot,在使用Spring boot应用程序将zip文件(包含两个RPM文件)从资源文件夹复制到目标文件夹时,我遇到了一个问题。它确实在目标文件夹路径上创建了zip文件,但它没有包含所有文件,并且创建的文件已损坏。请参阅附件 我已经通过了几个链接,但似乎解决方案并不完美 资源zip文件夹的快照 [ zip文件夹中文件的快照 代码: ClassPathResource cpr = null; cpr = new ClassPathResource("/16.00/packa

在使用Spring boot应用程序将zip文件(包含两个RPM文件)从资源文件夹复制到目标文件夹时,我遇到了一个问题。它确实在目标文件夹路径上创建了zip文件,但它没有包含所有文件,并且创建的文件已损坏。请参阅附件

我已经通过了几个链接,但似乎解决方案并不完美

  • 资源zip文件夹的快照 [

    zip文件夹中文件的快照

    代码:

     ClassPathResource cpr = null;
                cpr = new ClassPathResource("/16.00/package.zip");
                try {
                    InputStream data = cpr.getInputStream();
                    File targetFile = new File("C:\\package.zip");
                    java.nio.file.Files.copy(
                            data, 
                              targetFile.toPath(), 
                              StandardCopyOption.REPLACE_EXISTING);
                            IOUtils.closeQuietly(data);
                    log.info("w"+data);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
    上面的代码在目标文件夹上创建了一个zip文件,但它已损坏

    不知道为什么你认为你的代码会产生一个Zip文件,你只是把文件复制到一个文件中,这只会产生随机字节。你需要专门创建Zip文件和Zip流。下面是一个实用方法

    public static void addToZipFile(String zipFile, String[] filesToZip) throws Exception 
    {
        // Create a buffer for reading the files
        byte[] buf = new byte[1024];
    
        // Create the ZIP file
        String outFilename = zipFile;
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
    
        // Compress the files
        for (int i = 0; i < filesToZip.length; i++) 
        {
            FileInputStream in = new FileInputStream(filesToZip[i]);
    
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(FilenameUtils.getName(filesToZip[i])));
    
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) 
            {
                out.write(buf, 0, len);
            }
    
            // Complete the entry
            out.closeEntry();
            in.close();
        }
    
        // Complete the ZIP file
        out.close();
    
    }
    
    public static void addToZipFile(字符串zipFile,字符串[]filesToZip)引发异常
    {
    //创建用于读取文件的缓冲区
    字节[]buf=新字节[1024];
    //创建ZIP文件
    字符串outFilename=zipFile;
    ZipOutputStream out=newzipoutpstream(newfileoutputstream(outFilename));
    //压缩文件
    for(int i=0;i0)
    {
    out.write(buf,0,len);
    }
    //完成条目
    out.closeEntry();
    in.close();
    }
    //完成ZIP文件
    out.close();
    }
    
    我的这部分已经开始工作了。我可以使用上面的代码在zip中添加文件。问题是:如何从资源文件夹中读取zip文件并使用spring boot移动到任何目标文件夹中?