Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GZIP file Total length in C#

Tags:

c#

gzip

gunzip

I have a zipped file having size of several GBs, I want to get the size of Unzipped contents but don't want to actually unzip the file in C#, What might be the Library I can use? When I right click on the .gz file and go to Properties then under the Archive Tab there is a property name TotalLength which is showing this value. But I want to get it Programmatically using C#.. Any idea?

like image 358
Muhammad Ummar Avatar asked Jan 12 '11 06:01

Muhammad Ummar


People also ask

How do I check the size of a gzip file?

You can use the pretty-print and white-space only options to estimate the compression of non-minified content. If you need an estimate: Start with 100 JS files that have gone through the same minification pipeline. For each file, compute the ratio in sizes between gzip -c "$f" | wc -c and wc -c "$f"

How do I see the size of a compressed file?

Use the GetCompressedFileSize function to obtain the compressed size of a file. If the file is compressed, its compressed size will be less than its uncompressed size. Use the GetFileSize function to determine the uncompressed size of a file.

What is the extension of gzip file?

GZIP produces zipped files with the . gz extension. Although it's not commonly used on Windows, this compression format is still popular on UNIX/LINUX. If you receive a GZIP file, you can save it to your desktop and open it with WinZip.

How do I view the contents of a gz file in Unix?

If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.

What is the correct gzip command syntax?

The general syntax for the gzip command is as follows: gzip [OPTION]... [FILE]... Gzip compresses only single files and creates a compressed file for each given file.

How to get statistics about the size of a gzip file?

When used with the -l option, gzip shows statistics about the given compressed files: The output will include the uncompressed file name, the compressed and uncompressed size, and the compression ratio: To get more information, add the -v option:

How do I use gzip to compress multiple files?

Gzip compresses only single files and creates a compressed file for each given file. By convention, the name of a file compressed with Gzip should end with either .gz or .z. If you want to compress multiple files or directory into one file, first you need to create a Tar archive and then compress the .tar file with Gzip.

Does gzip keep the original file name and timestamp?

By default, gzipkeeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the -Noption. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.


1 Answers

The last 4 bytes of the gz file contains the length.

So it should be something like:

using(var fs = File.OpenRead(path))
{
  fs.Position = fs.Length - 4;
  var b = new byte[4];
  fs.Read(b, 0, 4);
  uint length = BitConverter.ToUInt32(b, 0);
  Console.WriteLine(length);
}
like image 173
leppie Avatar answered Sep 21 '22 12:09

leppie