Combine Streams (Compress a File)

9/25/2010 3:13:20 PM
Problem : You want to pump one stream’s output into another stream’s input to achieve complex behavior, such as compressing a file as you write it out.

Solution:You can combine streams to accomplish rich behavior that can perform powerful transformations. Listing 1 shows a quick-and-dirty file compression utility.

Listing 1. CompressFile.cs
using System;
using System.IO;
using System.IO.Compression;

namespace CompressFile
class Program
static bool compress = false;
static string sourceFile = null;
static string destFile = null;
const int BufferSize = 16384;

static void Main(string[] args)
if (!ParseArgs(args))
"CompressFile [compress|decompress] sourceFile destFile");
byte[] buffer = new byte[BufferSize];
/* First create regular streams to both source and
* destination files.
* Then create a gzip stream that encapsulates
* one or the other,depending on if we need to
* compress or uncompress.
* i.e. If we hook up the gzip stream to the
* output file streamand then write bytes to it,
* those bytes will be compressed.
using (Stream inFileStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outFileStream = File.Open(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipStream gzipStream = new GZipStream(
compress ? outFileStream : inFileStream,
compress ? CompressionMode.Compress
: CompressionMode.Decompress))
Stream inStream = compress?inFileStream:gzipStream;
Stream outStream = compress?gzipStream:outFileStream;

int bytesRead = 0;
bytesRead = inStream.Read(buffer, 0, BufferSize);
outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);

private static bool ParseArgs(string[] args)
if (args.Length < 3)
return false;

if (string.Compare(args[0], "compress") == 0)
compress = true;
else if (string.Compare(args[0], "decompress") == 0)
compress = false;
else return false;
sourceFile = args[1];
destFile = args[2];
return true;

  •  Create, Read, and Write a Binary File
  •  Create, Read, and Write a Text File
  •  Opening a Local File from a Silverlight Application
  •  Navigating Architecture Changes in Windows Vista
  •  Getting Around Windows Vista
  •  Managing User Account Control and Elevation Prompts
  •  Supporting Computers Running Windows Vista
  •  Using System Support Tools in Vista
  •  Managing System Properties
  •  Introducing Automated Help And Support in Vista
  •  Working with the Automated Help System
  •  Installing and Maintaining Devices in Vista: The Essentials
  •  Getting Started with Device Manager
  •  Working with Device in Vista
  •  Managing Hardware in Vista
  •  Customizing Hardware Device Settings
  •  Managing Internet Time in Vista
  •  Optimizing Windows Vista Menus
  •  Customizing the Taskbar in Vista
  •  Optimizing Toolbars in Vista
    Top 10
    Database Availability Group Replication in Exchange Server 2010 : Load Balancing in Exchange Server 2010
    .NET security : Isolated Storage Explained
    IIS 7.0 : Managing Configuration - Delegating Configuration (part 2)
    Personalizing Windows 7 (part 3) - Choosing and Configuring Your Screensaver
    Programming Windows Azure : Building a Storage Client
    Windows Azure : Messaging with the queue - Decoupling your system with messaging
    Windows Server 2008: DHCP/WINS/Domain Controllers - Exploring Global Catalog Domain Controller Placement
    SQL Azure : Tuning Techniques (part 1) - Dynamic Management Views
    Programming with DirectX : Shading and Surfaces - Types of Textures
    Managing Internet Explorer Security Zones
    Most View
    Protecting SharePoint 2010 from Viruses Using Forefront Protection 2010 for SharePoint
    Sharepoint 2007: Create a Personal Site
    Introducing Windows Phone 7 Photo Features (part 1) - Using a Chooser to Take Photos
    SQL Azure: Building a Shard (part 2) - Managing Database Connections
    Understanding Exchange Policy Enforcement Security : Creating Messaging Records Management Policies
    Programming Microsoft SQL Server 2005: Using Data Mining Extensions (part 1) - Data Mining Modeling Using DMX
    Defensive Database Programming with SQL Server : When Snapshot Isolation Breaks Code
    SharePoint 2010 : Understanding Advanced PowerShell Topics
    SQL Server 2008 : Managing Query Performance - Running the Standard Performance Reports
    Android Security : Broadcasts
    Embed a Web Browser in Your Application
    Introduction to Transport-Level Security in Windows Server 2008 R2
    Using Windows Phone 7 Technologies : Understanding Orientation and Movement
    Building Out Of Browser Silverlight Applications
    Worker Processes, Application Pools, and Identities in IIS 7
    Business Intelligence in SharePoint 2010 with Business Connectivity Services : External Content Types (part 1)
    Mobile Phone Game Programming : Java As a Mobile Game Platform
    Windows 7 : Windows Driver Foundation Architecture (part 2) - Integrated I/O Queuing and Cancellation
    Collaborating Within an Exchange Server Environment Using Microsoft Office SharePoint Server 2007 : Customizing and Developing MOSS Sites
    SQL Server System and Database Administration : System Tables & System Stored Procedures