Why do I only get the first few lines of file when using GZipStream to decompress in dotnet core











up vote
0
down vote

favorite












I'm trying to read a series of gzip files using the code below.



What I end up with is a file that is only a fraction of the total file. Interestingly enough the text file output is almost perfectly truncated at a line break.



public static string UnzipFile(string filename)
{
FileInfo fi = new FileInfo(filename);
Console.WriteLine($" - - - - - UnzipFile {filename} {fi.Length} - - - - - ");
string newFileName = filename.Substring(0, filename.Length - 4);

try
{
byte buffer = new byte[4096]; // read in chunks of 4KB
using (FileStream fs2 = new FileStream(newFileName, FileMode.CreateNew))
{
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress))
{
zipStream.BufferSize = 1024;
int bytesRead;
while((bytesRead = zipStream.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine($" {bytesRead} ");
fs2.Write(buffer, 0, bytesRead);
fs2.Flush(true);
}
}
}
}
FileInfo fi2 = new FileInfo(newFileName);
Console.WriteLine($" - - - - {newFileName} {fi2.Length}");
}
catch (Exception e)
{
Console.WriteLine($" - - - - - EXCEPTION {e.Message} {e.StackTrace}");
}
return newFileName;
}


Here is partial output from running the code:



UnzipFile : 177686_2018-10-18_0#289.json.gz 34975
4096
4096
855
177686_2018-10-18_0#289.json 9047


So it looks like a file of size 34975 bytes when compressed ends up decompressing only 9047 bytes. If I use gunzip on a mac to expand the file I get a file 223344 bytes in size.



Where have I gone wrong?



edit: Additional data



In case it helps, I get this when using GZip.Decompress



Bad CRC32 in GZIP trailer. (actual(7C64AEAA)!=expected(F641B726)) at Ionic.Zlib.ZlibBaseStream.finish()


Additional data when examining the position of the FileStream



fs pos: 2093 fs len: 33930









share|improve this question
























  • By the way, streams have a CopyTo method :)
    – John
    Nov 15 at 2:03










  • Are you able to attach an example input to your code so people can test it?
    – mjwills
    Nov 15 at 2:23










  • @mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
    – Zain Mecklai
    Nov 15 at 2:42






  • 1




    @John Thanks! I'll incorporate that! Appreciate the tip.
    – Zain Mecklai
    Nov 15 at 2:42















up vote
0
down vote

favorite












I'm trying to read a series of gzip files using the code below.



What I end up with is a file that is only a fraction of the total file. Interestingly enough the text file output is almost perfectly truncated at a line break.



public static string UnzipFile(string filename)
{
FileInfo fi = new FileInfo(filename);
Console.WriteLine($" - - - - - UnzipFile {filename} {fi.Length} - - - - - ");
string newFileName = filename.Substring(0, filename.Length - 4);

try
{
byte buffer = new byte[4096]; // read in chunks of 4KB
using (FileStream fs2 = new FileStream(newFileName, FileMode.CreateNew))
{
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress))
{
zipStream.BufferSize = 1024;
int bytesRead;
while((bytesRead = zipStream.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine($" {bytesRead} ");
fs2.Write(buffer, 0, bytesRead);
fs2.Flush(true);
}
}
}
}
FileInfo fi2 = new FileInfo(newFileName);
Console.WriteLine($" - - - - {newFileName} {fi2.Length}");
}
catch (Exception e)
{
Console.WriteLine($" - - - - - EXCEPTION {e.Message} {e.StackTrace}");
}
return newFileName;
}


Here is partial output from running the code:



UnzipFile : 177686_2018-10-18_0#289.json.gz 34975
4096
4096
855
177686_2018-10-18_0#289.json 9047


So it looks like a file of size 34975 bytes when compressed ends up decompressing only 9047 bytes. If I use gunzip on a mac to expand the file I get a file 223344 bytes in size.



Where have I gone wrong?



edit: Additional data



In case it helps, I get this when using GZip.Decompress



Bad CRC32 in GZIP trailer. (actual(7C64AEAA)!=expected(F641B726)) at Ionic.Zlib.ZlibBaseStream.finish()


Additional data when examining the position of the FileStream



fs pos: 2093 fs len: 33930









share|improve this question
























  • By the way, streams have a CopyTo method :)
    – John
    Nov 15 at 2:03










  • Are you able to attach an example input to your code so people can test it?
    – mjwills
    Nov 15 at 2:23










  • @mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
    – Zain Mecklai
    Nov 15 at 2:42






  • 1




    @John Thanks! I'll incorporate that! Appreciate the tip.
    – Zain Mecklai
    Nov 15 at 2:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to read a series of gzip files using the code below.



What I end up with is a file that is only a fraction of the total file. Interestingly enough the text file output is almost perfectly truncated at a line break.



public static string UnzipFile(string filename)
{
FileInfo fi = new FileInfo(filename);
Console.WriteLine($" - - - - - UnzipFile {filename} {fi.Length} - - - - - ");
string newFileName = filename.Substring(0, filename.Length - 4);

try
{
byte buffer = new byte[4096]; // read in chunks of 4KB
using (FileStream fs2 = new FileStream(newFileName, FileMode.CreateNew))
{
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress))
{
zipStream.BufferSize = 1024;
int bytesRead;
while((bytesRead = zipStream.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine($" {bytesRead} ");
fs2.Write(buffer, 0, bytesRead);
fs2.Flush(true);
}
}
}
}
FileInfo fi2 = new FileInfo(newFileName);
Console.WriteLine($" - - - - {newFileName} {fi2.Length}");
}
catch (Exception e)
{
Console.WriteLine($" - - - - - EXCEPTION {e.Message} {e.StackTrace}");
}
return newFileName;
}


Here is partial output from running the code:



UnzipFile : 177686_2018-10-18_0#289.json.gz 34975
4096
4096
855
177686_2018-10-18_0#289.json 9047


So it looks like a file of size 34975 bytes when compressed ends up decompressing only 9047 bytes. If I use gunzip on a mac to expand the file I get a file 223344 bytes in size.



Where have I gone wrong?



edit: Additional data



In case it helps, I get this when using GZip.Decompress



Bad CRC32 in GZIP trailer. (actual(7C64AEAA)!=expected(F641B726)) at Ionic.Zlib.ZlibBaseStream.finish()


Additional data when examining the position of the FileStream



fs pos: 2093 fs len: 33930









share|improve this question















I'm trying to read a series of gzip files using the code below.



What I end up with is a file that is only a fraction of the total file. Interestingly enough the text file output is almost perfectly truncated at a line break.



public static string UnzipFile(string filename)
{
FileInfo fi = new FileInfo(filename);
Console.WriteLine($" - - - - - UnzipFile {filename} {fi.Length} - - - - - ");
string newFileName = filename.Substring(0, filename.Length - 4);

try
{
byte buffer = new byte[4096]; // read in chunks of 4KB
using (FileStream fs2 = new FileStream(newFileName, FileMode.CreateNew))
{
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Decompress))
{
zipStream.BufferSize = 1024;
int bytesRead;
while((bytesRead = zipStream.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine($" {bytesRead} ");
fs2.Write(buffer, 0, bytesRead);
fs2.Flush(true);
}
}
}
}
FileInfo fi2 = new FileInfo(newFileName);
Console.WriteLine($" - - - - {newFileName} {fi2.Length}");
}
catch (Exception e)
{
Console.WriteLine($" - - - - - EXCEPTION {e.Message} {e.StackTrace}");
}
return newFileName;
}


Here is partial output from running the code:



UnzipFile : 177686_2018-10-18_0#289.json.gz 34975
4096
4096
855
177686_2018-10-18_0#289.json 9047


So it looks like a file of size 34975 bytes when compressed ends up decompressing only 9047 bytes. If I use gunzip on a mac to expand the file I get a file 223344 bytes in size.



Where have I gone wrong?



edit: Additional data



In case it helps, I get this when using GZip.Decompress



Bad CRC32 in GZIP trailer. (actual(7C64AEAA)!=expected(F641B726)) at Ionic.Zlib.ZlibBaseStream.finish()


Additional data when examining the position of the FileStream



fs pos: 2093 fs len: 33930






c# .net gzip sharpziplib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 14:32

























asked Nov 15 at 1:47









Zain Mecklai

64




64












  • By the way, streams have a CopyTo method :)
    – John
    Nov 15 at 2:03










  • Are you able to attach an example input to your code so people can test it?
    – mjwills
    Nov 15 at 2:23










  • @mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
    – Zain Mecklai
    Nov 15 at 2:42






  • 1




    @John Thanks! I'll incorporate that! Appreciate the tip.
    – Zain Mecklai
    Nov 15 at 2:42


















  • By the way, streams have a CopyTo method :)
    – John
    Nov 15 at 2:03










  • Are you able to attach an example input to your code so people can test it?
    – mjwills
    Nov 15 at 2:23










  • @mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
    – Zain Mecklai
    Nov 15 at 2:42






  • 1




    @John Thanks! I'll incorporate that! Appreciate the tip.
    – Zain Mecklai
    Nov 15 at 2:42
















By the way, streams have a CopyTo method :)
– John
Nov 15 at 2:03




By the way, streams have a CopyTo method :)
– John
Nov 15 at 2:03












Are you able to attach an example input to your code so people can test it?
– mjwills
Nov 15 at 2:23




Are you able to attach an example input to your code so people can test it?
– mjwills
Nov 15 at 2:23












@mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
– Zain Mecklai
Nov 15 at 2:42




@mjwills I have proprietary data in the files so that's a blocker for me. I'll see if I can scrub some of it. If it helps, I get it from Amplitude's API amplitude.zendesk.com/hc/en-us/articles/…
– Zain Mecklai
Nov 15 at 2:42




1




1




@John Thanks! I'll incorporate that! Appreciate the tip.
– Zain Mecklai
Nov 15 at 2:42




@John Thanks! I'll incorporate that! Appreciate the tip.
– Zain Mecklai
Nov 15 at 2:42

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311294%2fwhy-do-i-only-get-the-first-few-lines-of-file-when-using-gzipstream-to-decompres%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311294%2fwhy-do-i-only-get-the-first-few-lines-of-file-when-using-gzipstream-to-decompres%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?