You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2020/10/28 18:01:00 UTC

[jira] [Comment Edited] (IO-691) IOUtils copyLarge gives incorrect result with buffered stream and zip file

    [ https://issues.apache.org/jira/browse/IO-691?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17222109#comment-17222109 ] 

Alex Herbert edited comment on IO-691 at 10/28/20, 6:00 PM:
------------------------------------------------------------

The IOUtils.copy methods do not close the input or output streams. This allows the output stream to be used for more writing after the copy has completed.

I would guess that the BufferedOutputStream still has data in the buffer and needs to be closed. Try this which uses a try-with-resources to auto close the input and output streams:
{code:java}
        String oldFilePath = "your_path/test.zip";
        String newFilePath = "your_path/new_zip.zip";
        try (InputStream in = new FileInputStream(oldFilePath);
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFilePath))) {
            IOUtils.copyLarge(in, out);
        }
{code}


was (Author: alexherbert):
The IOUtils.copy methods do not close the input or output streams. This allows the output stream to be used for more writing after the copy has completed.

I would guess that the BufferedOutputStream still has data in the buffer and needs to be closed. Try this which uses a try-with-resources to auto close the input and output streams:
{code:java}
        String oldFilePath = "your_path/test.zip";
        String newFilePath = "your_path/new_zip.zip";
        try (InputStream in = new FileInputStream(oldFilePath);
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFilePath)) {
            IOUtils.copyLarge(in, out);
        }
{code}

> IOUtils copyLarge gives incorrect result with buffered stream and zip file
> --------------------------------------------------------------------------
>
>                 Key: IO-691
>                 URL: https://issues.apache.org/jira/browse/IO-691
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.8.0
>         Environment: Mac OS Catalina v 10.15.7 
> Java 11 
> MacBook Pro (16-inch, 2019)
>            Reporter: Aymon de Broglie
>            Priority: Minor
>         Attachments: test.zip
>
>
> I run the following code 
>  
> {code:java}
> import org.apache.commons.io.IOUtils;
> import java.io.*;
> class Scratch {
>     public static void main(String[] args) throws IOException {
>         String oldFilePath = "your_path/test.zip";
>         String newFilePath = "your_path/new_zip.zip";
>         InputStream inputStream = new FileInputStream(oldFilePath);
>         File file = new File(newFilePath);
>         file.createNewFile();
>         IOUtils.copyLarge(inputStream, new BufferedOutputStream(new FileOutputStream(file)));
>     }
> }
> {code}
> I then try to open the new_zip.zip and it is impossible to open, the file is not well copied
> However if I remove the BufferedOutputStream as below, the copy works correctly 
>  {code:java}
> import org.apache.commons.io.IOUtils;
> import java.io.*;
> class Scratch {
>     public static void main(String[] args) throws IOException {
>         String oldFilePath = "your_path/test.zip";
>         String newFilePath = "your_path/new_zip.zip";
>         InputStream inputStream = new FileInputStream(oldFilePath);
>         File file = new File(newFilePath);
>         file.createNewFile();
>         IOUtils.copyLarge(inputStream, new FileOutputStream(file));
>     }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)