phaphe created COMPRESS-198:
-------------------------------

             Summary: ZipArchiveInputStream truncates file in some condition
                 Key: COMPRESS-198
                 URL: https://issues.apache.org/jira/browse/COMPRESS-198
             Project: Commons Compress
          Issue Type: Bug
    Affects Versions: 1.4.1
            Reporter: phaphe


ZipArchiveInputStream in some special situation truncates file silently without 
warning. The problem occurs when the source stream is a stream which does not 
support mark function and this stream is encapsulated in BufferedInputStream.
This behaviour can be easily reproduced.

The test code:
{code:title=Test.java|borderStyle=solid}
/**
 * Unzips test file and demonstrates the problem.
 * 
 *
 */
public class Test {

    /**
     * Silly Stream.
     * It simulates situation, when mark function is not supported.
     *
     */
    public static class SillyStream extends InputStream {
        FileInputStream source;
        
        public SillyStream(FileInputStream source) {
            this.source = source;
        }

        @Override
        public int read() throws IOException {
            return source.read();
        }
        
        @Override
        public void close() throws IOException {
            source.close();
        }
    }

    public static void main(String[] args) throws IOException {
        File exportDirectory = new File("out");
        
        FileInputStream fis = new FileInputStream("test.zip");
        //encapsulating with silly stream to simulate situation when mark 
function is not supported
        SillyStream ss = new SillyStream(fis);
        //encapsulating with buffered stream - Buffered stream supports mark 
function
        BufferedInputStream bs = new BufferedInputStream(ss,30000);
        
        ZipArchiveInputStream zip = new ZipArchiveInputStream(bs);
        ZipArchiveEntry entry;
        while ((entry = zip.getNextZipEntry()) != null) {
            File newFile = new File(
                    exportDirectory.getAbsolutePath() + "/"
                            + entry.getName());
            if (entry.isDirectory()) {
                createDir(newFile);
                continue;
            }
            createFile(newFile);
            FileOutputStream fos = new FileOutputStream(newFile);
            BufferedOutputStream bfos = new BufferedOutputStream(
                    fos);
            
            //Buffered input stream is necessary there to simulate the 
            //problem
            InputStream is = new BufferedInputStream(zip);
            int ch;
            
            //silly reading byte by byte
            while((ch = is.read()) != -1) {
                bfos.write(ch);
            }
            
            bfos.flush();
            bfos.close();
        }
        zip.close();

    }

    private static void createFile(File file) throws IOException {
        System.out.println("File:" + file.getAbsolutePath());
                
        File parent = file.getParentFile();
        parent.mkdirs();
              
        file.createNewFile();        
    }
    
    private static void createDir(File file) throws IOException {
        System.out.println("Dir:" + file.getAbsolutePath());
        file.mkdirs();
    }
}

{code}

The test file is there (300MB):
http://uloz.to/x9kJLrt/test-zip

The ZIP file was created by common compress.

The problem can be found in file txt/1_0050.tif.txt. The real size is 4300 
bytes but after decompress with java code it has only 4240 bytes.


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to