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
c# .net gzip sharpziplib
add a comment |
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
c# .net gzip sharpziplib
By the way, streams have aCopyTo
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
add a comment |
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
c# .net gzip sharpziplib
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
c# .net gzip sharpziplib
edited Nov 15 at 14:32
asked Nov 15 at 1:47
Zain Mecklai
64
64
By the way, streams have aCopyTo
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
add a comment |
By the way, streams have aCopyTo
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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