RarArchive Entry Stream not Copying correctly #476

Open
opened 2026-01-29 22:12:38 +00:00 by claunia · 17 comments
Owner

Originally created by @CBeatt13 on GitHub (Sep 26, 2021).

Having an issue extracting a RAR archive entry to another stream. If I extract the whole archive the files extract correctly. If I attempt to extract a single entry to either a file or I open the entry stream and then copy that to another stream the file is corrupted. The file I'm extracting is a JPG image, when I attempt to extract the single image the image renders the top 20 lines or so of pixels then the rest of the image is all saved as grey. This only seems to happen sporadically but does happen pretty often for me.

Originally created by @CBeatt13 on GitHub (Sep 26, 2021). Having an issue extracting a RAR archive entry to another stream. If I extract the whole archive the files extract correctly. If I attempt to extract a single entry to either a file or I open the entry stream and then copy that to another stream the file is corrupted. The file I'm extracting is a JPG image, when I attempt to extract the single image the image renders the top 20 lines or so of pixels then the rest of the image is all saved as grey. This only seems to happen sporadically but does happen pretty often for me.
Author
Owner

@CBeatt13 commented on GitHub (Sep 26, 2021):

Adding some more potentially useful information:

Version to extract on the archive in question is 2.0 (According to WinRAR)
SharpCompress reads that the entry in question is complete
Archive isn't password protected at all
Have the issue both when reading the first entity immediately, as well as right after using a LINQ statement on all entries to re-order (which I assume is reading all the entities)

@CBeatt13 commented on GitHub (Sep 26, 2021): Adding some more potentially useful information: Version to extract on the archive in question is 2.0 (According to WinRAR) SharpCompress reads that the entry in question is complete Archive isn't password protected at all Have the issue both when reading the first entity immediately, as well as right after using a LINQ statement on all entries to re-order (which I assume is reading all the entities)
Author
Owner

@adamhathcock commented on GitHub (Sep 27, 2021):

Without seeing any code, it sounds similar to this problem: https://github.com/adamhathcock/sharpcompress/issues/620

@adamhathcock commented on GitHub (Sep 27, 2021): Without seeing any code, it sounds similar to this problem: https://github.com/adamhathcock/sharpcompress/issues/620
Author
Owner

@CBeatt13 commented on GitHub (Sep 27, 2021):

I'll get ya a code snippet to show what I'm referring to. Main differences between this and #620 are the following:
I have the issue exclusively with RAR archives
The issue doesn't happen for ALL archives
I don't have to iterate or manipulate the Entry enumerable to see the issue in question
I'm not attempting to iterate the archive's entry enumerable after navigating to the first entry

@CBeatt13 commented on GitHub (Sep 27, 2021): I'll get ya a code snippet to show what I'm referring to. Main differences between this and #620 are the following: I have the issue exclusively with RAR archives The issue doesn't happen for ALL archives I don't have to iterate or manipulate the Entry enumerable to see the issue in question I'm not attempting to iterate the archive's entry enumerable after navigating to the first entry
Author
Owner

@CBeatt13 commented on GitHub (Sep 27, 2021):

Code I'm using that generates the issue, it's worth noting I can make it happen still after removing the LINQ query:


foreach (var entry in archive.Entries.Where(e => !e.IsDirectory && e.Key.ToLower().EndsWith(".jpg"))
    .OrderBy(e => e.Key.Contains(Path.DirectorySeparatorChar) ? 0 : 1)
    .ThenBy(e => e.Key))
{
    string fileName = string.Join('_', entry.Key.Split(Path.GetInvalidFileNameChars()));
    imagePath = Path.Combine(Paths.TempDirectory, fileName);
    var tempStream = File.Open(imagePath, FileMode.Create, FileAccess.Write);
    var entryStream = entry.OpenEntryStream();
    entryStream.CopyTo(tempStream);
    entryStream.Close();
    entryStream.Dispose();
    tempStream.Close();
    tempStream.Dispose();

    return true;
}

@CBeatt13 commented on GitHub (Sep 27, 2021): Code I'm using that generates the issue, it's worth noting I can make it happen still after removing the LINQ query: ```var archive = ArchiveFactory.Open(archivePath); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory && e.Key.ToLower().EndsWith(".jpg")) .OrderBy(e => e.Key.Contains(Path.DirectorySeparatorChar) ? 0 : 1) .ThenBy(e => e.Key)) { string fileName = string.Join('_', entry.Key.Split(Path.GetInvalidFileNameChars())); imagePath = Path.Combine(Paths.TempDirectory, fileName); var tempStream = File.Open(imagePath, FileMode.Create, FileAccess.Write); var entryStream = entry.OpenEntryStream(); entryStream.CopyTo(tempStream); entryStream.Close(); entryStream.Dispose(); tempStream.Close(); tempStream.Dispose(); return true; }
Author
Owner

@CBeatt13 commented on GitHub (Sep 27, 2021):

Later tonight if you don't have an idea what may be causing the issue I'll pull down the code and see if I can help track down the cause

@CBeatt13 commented on GitHub (Sep 27, 2021): Later tonight if you don't have an idea what may be causing the issue I'll pull down the code and see if I can help track down the cause
Author
Owner

@CBeatt13 commented on GitHub (Sep 27, 2021):

Just used the same file in your ComicBlaze project (since it's basically doing the same thing) and the issue exists there as well for the file in question

@CBeatt13 commented on GitHub (Sep 27, 2021): Just used the same file in your ComicBlaze project (since it's basically doing the same thing) and the issue exists there as well for the file in question
Author
Owner

@adamhathcock commented on GitHub (Sep 27, 2021):

If you do ToList() on the foreach, does it still happen?

@adamhathcock commented on GitHub (Sep 27, 2021): If you do `ToList()` on the foreach, does it still happen?
Author
Owner

@CBeatt13 commented on GitHub (Sep 27, 2021):

No change when adding .ToList(). Also still does it if I remove the ForEach entirely and just call archive.Entries.First then run the logic accordingly.

@CBeatt13 commented on GitHub (Sep 27, 2021): No change when adding .ToList(). Also still does it if I remove the ForEach entirely and just call archive.Entries.First then run the logic accordingly.
Author
Owner

@CBeatt13 commented on GitHub (Sep 28, 2021):

Minimum code required to get this issue is below. File is a CBR with copyright material in it so can't directly attach it to the ticket, but can get it to ya if you need.

var archive = RarArchive.Open({filepathToRar});
archive.Entries.First().WriteToFile({destinationFilePath});
@CBeatt13 commented on GitHub (Sep 28, 2021): Minimum code required to get this issue is below. File is a CBR with copyright material in it so can't directly attach it to the ticket, but can get it to ya if you need. ``` var archive = RarArchive.Open({filepathToRar}); archive.Entries.First().WriteToFile({destinationFilePath});
Author
Owner

@CBeatt13 commented on GitHub (Sep 28, 2021):

Tried to step through but there is enough work being done moving stuff back and forth between streams and buffers that I'm not even sure where to begin pin-pointing what could be the culprit. If there is anything more you need me to try and test though let me know and I'll do my best with it.

@CBeatt13 commented on GitHub (Sep 28, 2021): Tried to step through but there is enough work being done moving stuff back and forth between streams and buffers that I'm not even sure where to begin pin-pointing what could be the culprit. If there is anything more you need me to try and test though let me know and I'll do my best with it.
Author
Owner

@CBeatt13 commented on GitHub (Sep 30, 2021):

Been looking more into this and it only appears to be an issue with a version 2.0 RAR archive, and only when extracting individual entries, extracting the entire archive works as expected.

@CBeatt13 commented on GitHub (Sep 30, 2021): Been looking more into this and it only appears to be an issue with a version 2.0 RAR archive, and only when extracting individual entries, extracting the entire archive works as expected.
Author
Owner

@adamhathcock commented on GitHub (Oct 1, 2021):

A RAR 2.0 file is something that's pretty old now, isn't it? I'm just wondering how to narrow this down

@adamhathcock commented on GitHub (Oct 1, 2021): A RAR 2.0 file is something that's pretty old now, isn't it? I'm just wondering how to narrow this down
Author
Owner

@CBeatt13 commented on GitHub (Oct 1, 2021):

The codebase has a switch in that it uses to perform stream copy operations based on the RAR format and 2.0 has it's own case in that switch, so would imagine that'd be a good start. Compare that logic to the logic used to extract the entire archive since that IS working. I'd imagine there is a unintended difference in the two parts of code. At work right now so can't get you the exact file/line number of the switch I'm referring to, but can tonight once home.

@CBeatt13 commented on GitHub (Oct 1, 2021): The codebase has a switch in that it uses to perform stream copy operations based on the RAR format and 2.0 has it's own case in that switch, so would imagine that'd be a good start. Compare that logic to the logic used to extract the entire archive since that IS working. I'd imagine there is a unintended difference in the two parts of code. At work right now so can't get you the exact file/line number of the switch I'm referring to, but can tonight once home.
Author
Owner

@nausixkiz commented on GitHub (Dec 23, 2021):

Same issue

@nausixkiz commented on GitHub (Dec 23, 2021): Same issue
Author
Owner

@adamhathcock commented on GitHub (Dec 24, 2021):

Creating a test with a matching file would probably help here. If a broken test was in a PR, then me or someone else could fix it.

@adamhathcock commented on GitHub (Dec 24, 2021): Creating a test with a matching file would probably help here. If a broken test was in a PR, then me or someone else could fix it.
Author
Owner

@Nanook commented on GitHub (Feb 15, 2022):

https://github.com/adamhathcock/sharpcompress/pull/638

I hope this solves this issue.

@Nanook commented on GitHub (Feb 15, 2022): https://github.com/adamhathcock/sharpcompress/pull/638 I hope this solves this issue.
Author
Owner

@adamhathcock commented on GitHub (Mar 30, 2022):

Fix released in https://www.nuget.org/packages/SharpCompress/0.31.0

@adamhathcock commented on GitHub (Mar 30, 2022): Fix released in https://www.nuget.org/packages/SharpCompress/0.31.0
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#476