[ 
https://issues.apache.org/jira/browse/COMPRESS-496?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

yiang li updated COMPRESS-496:
------------------------------
    Description: 
When extracting an empty file in SevenZFile, an exception will be thrown. This 
is because the `deferredBlockStreams` in SevenZFile was clear here
{code:java}
if (folderIndex < 0) {
 deferredBlockStreams.clear();
 // TODO: previously it'd return an empty stream?
 // new BoundedInputStream(new ByteArrayInputStream(new byte[0]), 0);
 return;
}{code}
Maybe an empty stream should be added to deferredBlockStreams here as the TODO 
said?

 

Code to reproduce the exception
{code:java}
byte[] buffer = new byte[4096];
 String destination = "/output/dir/of/7z";
 SevenZFile sevenZFile = new SevenZFile(new File("/file/to/extract.7z"));
 SevenZArchiveEntry entry;
 while ((entry = sevenZFile.getNextEntry()) != null){
 if (entry.isDirectory())
{ continue; }
File curfile = new File(destination, entry.getName());
 File parent = curfile.getParentFile();
 if (!parent.exists())
{ parent.mkdirs(); }
FileOutputStream out = new FileOutputStream(curfile);
while(sevenZFile.read(buffer, 0, buffer.length) > 0)
{ out.write(buffer); out.close(); }
}
{code}
BTW it's OK with the sample code in user guide of commons-compress because the 
sample code is using entry's size as the size of the buffer, which is 0. But in 
most cases, we need to reuse the buffer instead of creating it each time I need 
it.

  was:
When extracting an empty file in SevenZFile, an exception will be thrown. This 
is because the `deferredBlockStreams` in SevenZFile was clear here
{code:java}
if (folderIndex < 0) {
 deferredBlockStreams.clear();
 // TODO: previously it'd return an empty stream?
 // new BoundedInputStream(new ByteArrayInputStream(new byte[0]), 0);
 return;
}{code}
Maybe an empty stream should be added to deferredBlockStreams here as the TODO 
said?

 

Code to reproduce the exception
{code:java}
byte[] buffer = new byte[4096];
 String destination = "/output/dir/of/7z";
 SevenZFile sevenZFile = new SevenZFile(new File("/file/to/extract.7z"));
 SevenZArchiveEntry entry;
 while ((entry = sevenZFile.getNextEntry()) != null){
 if (entry.isDirectory())
{ continue; }
File curfile = new File(destination, entry.getName());
 File parent = curfile.getParentFile();
 if (!parent.exists())
{ parent.mkdirs(); }
FileOutputStream out = new FileOutputStream(curfile);
while(sevenZFile.read(buffer, 0, buffer.length) > 0)
{ out.write(buffer); out.close(); }
}
{code}


> SevenZFile.read got an exception when extracting empty file
> -----------------------------------------------------------
>
>                 Key: COMPRESS-496
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-496
>             Project: Commons Compress
>          Issue Type: Bug
>            Reporter: yiang li
>            Priority: Minor
>
> When extracting an empty file in SevenZFile, an exception will be thrown. 
> This is because the `deferredBlockStreams` in SevenZFile was clear here
> {code:java}
> if (folderIndex < 0) {
>  deferredBlockStreams.clear();
>  // TODO: previously it'd return an empty stream?
>  // new BoundedInputStream(new ByteArrayInputStream(new byte[0]), 0);
>  return;
> }{code}
> Maybe an empty stream should be added to deferredBlockStreams here as the 
> TODO said?
>  
> Code to reproduce the exception
> {code:java}
> byte[] buffer = new byte[4096];
>  String destination = "/output/dir/of/7z";
>  SevenZFile sevenZFile = new SevenZFile(new File("/file/to/extract.7z"));
>  SevenZArchiveEntry entry;
>  while ((entry = sevenZFile.getNextEntry()) != null){
>  if (entry.isDirectory())
> { continue; }
> File curfile = new File(destination, entry.getName());
>  File parent = curfile.getParentFile();
>  if (!parent.exists())
> { parent.mkdirs(); }
> FileOutputStream out = new FileOutputStream(curfile);
> while(sevenZFile.read(buffer, 0, buffer.length) > 0)
> { out.write(buffer); out.close(); }
> }
> {code}
> BTW it's OK with the sample code in user guide of commons-compress because 
> the sample code is using entry's size as the size of the buffer, which is 0. 
> But in most cases, we need to reuse the buffer instead of creating it each 
> time I need it.



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

Reply via email to