Skip to content

FileInfo.ExtractGZipToDirectory Not Working #19

@adamfisher

Description

@adamfisher

I was unable to get ExtractGZipToDirectory() working. It just created an empty file when I tried to decompress it. I ended up creating this method which did work:

private static FileInfo DecompressGZipFile(this FileInfo compressedFilePath)
{
	using (var fs = compressedFilePath.OpenRead())
	{
		using (var zipStream = new GZipStream(fs, CompressionMode.Decompress))
		{
			var buffer1 = compressedFilePath.Name.Split('.');
			var buffer2 = buffer1[0] + "." + buffer1[1];
			var filePath = Path.Combine(compressedFilePath.DirectoryName, buffer2);

			using (var fOutStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
			{
				var tempBytes = new byte[4096];
				int i;
				while ((i = zipStream.Read(tempBytes, 0, tempBytes.Length)) != 0)
				{
					fOutStream.Write(tempBytes, 0, i);
				}
			}

			return new FileInfo(filePath);
		}
	}
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions