You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "yiang li (Jira)" <ji...@apache.org> on 2019/10/18 08:42:00 UTC
[jira] [Updated] (COMPRESS-496) SevenZFile.read got an exception
when extracting empty file
[ 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}
was:
{color:#FF0000}{color}When extracting an empty file in SevenZFile, an exception will be thrown. This is because the `deferredBlockStreams` in SevenZFile was clear here
```
if (folderIndex < 0) {
deferredBlockStreams.clear();
// TODO: previously it'd return an empty stream?
// new BoundedInputStream(new ByteArrayInputStream(new byte[0]), 0);
return;
}
```
Maybe an empty stream should be added to deferredBlockStreams here as the TODO said?
Code to reproduce the exception
```
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();
}
}
```
> 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}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)