473,670 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem writing compressed data to disk

I am exploring the DeflateStream class as a practice. I thought it is
fun to compress a text file and write it to a disk file. So I created
the following method. If you don't bother reading the small code
snippet, here is a summary of what I did:

1. Read the text content into a buffer through FileStream.Read .
2. Create a DeflateStream by wrapping up a MemoryStream.
3. Write the compressed data into the underline stream (the
MemoryStream in this case) through DeflateStream.W rite
4. Create a FileStream for write.
5. Call the MemoryStream's Write method to dump the compressed data
into the output FileStream.

Pretty straight forward, isn't it? It compiles and runs OK, but the
compressed data isn't correct. All I am getting is this:

http://gnewsgroup.googlepages.com/compressionproblem

And after unzipping this data, I get a text file which seems to
contain only spaces. Just in case you wonder, my UnzipFile method
works fine with correctly zipped data.

Am I going through the right procedure of compressing a file as
indicated 1 through 5 above? Any hint? Thank you.

public static void CompressFile(st ring source, string dest)
{
FileStream infile, outfile;
DeflateStream zipStream;
try
{
infile = new FileStream(sour ce, FileMode.Open,
FileAccess.Read );
byte[] buffer = new byte[infile.Length];
int count = infile.Read(buf fer, 0, buffer.Length);

// Create a DeflateStream instance.
MemoryStream ms = new MemoryStream();
zipStream = new DeflateStream(m s,
CompressionMode .Compress);

// The follwoing call writes the compressed data in buffer
// to the underline stream, in this case, the MemoryStream
ms.
zipStream.Write (buffer, 0, buffer.Length);

// Create a file stream for writing.
outfile = new FileStream(dest , FileMode.Create ,
FileAccess.Writ e);

// Now, dump the zippped data to the file stream just
created.
ms.WriteTo(outf ile);

zipStream.Dispo se();
outfile.Dispose ();
ms.Dispose();
}
catch (IOException ioe)
{
Console.WriteLi ne(ioe.Message) ;
}
}

Jun 27 '08 #1
2 2265
Well, that doesn't really make best use of the stream; really you want
to use a small buffer, rather than reading the entire file into
memory. maybe something more like below?

Marc

static void CompressFile(st ring source, string dest)
{

using(Stream inStream = File.OpenRead(s ource))
using(Stream outStream = File.Create(des t))
using (DeflateStream zip = new DeflateStream(o utStream,
CompressionMode .Compress))
{
const int BUFFER_LEN = 8 * 1024; // 8K
byte[] buffer = new byte[BUFFER_LEN];
int bytesRead;
while ((bytesRead = inStream.Read(b uffer, 0,
BUFFER_LEN)) 0)
{
zip.Write(buffe r, 0, bytesRead);
}
inStream.Close( );
zip.Close();
outStream.Close ();
}
}
Jun 27 '08 #2
On Jun 23, 5:21*pm, Marc Gravell <marc.grav...@g mail.comwrote:
Well, that doesn't really make best use of the stream; really you want
to use a small buffer, rather than reading the entire file into
memory. maybe something more like below?

Marc

static voidCompressFil e(string source, string dest)
* * * * {

* * * * * * using(Stream inStream = File.OpenRead(s ource))
* * * * * * using(Stream outStream = File.Create(des t))
* * * * * * using (DeflateStream zip = new DeflateStream(o utStream,
CompressionMode .Compress))
* * * * * * {
* * * * * * * * const int BUFFER_LEN = 8 * 1024; // 8K
* * * * * * * * byte[] buffer = new byte[BUFFER_LEN];
* * * * * * * * int bytesRead;
* * * * * * * * while ((bytesRead = inStream.Read(b uffer, 0,
BUFFER_LEN)) 0)
* * * * * * * * {
* * * * * * * * * * zip.Write(buffe r, 0, bytesRead);
* * * * * * * * }
* * * * * * * * inStream.Close( );
* * * * * * * * zip.Close();
* * * * * * * * outStream.Close ();
* * * * * * }
* * * * }
Thank you very much. I tried your code, and it works perfectly. I am
sorta confused why mine does not work.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1574
by: Oliver Spiesshofer | last post by:
Hi, when compressing strings with bzcompress, I have the problem that the result apparently often contains 'letters' that mess up sql statements. Of course I can urlencode the string, but that wont keep it as small. What would be the best way to send a bzcompressed string to a SQL statement? is it possible at all without urlencode? thanks
7
3773
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two days and would appreciate this group's helpful expertise. My problem may be related to mixing the C and C++ languages together. Namely, I have a struct which I cannot change (as legacy code) similiar to this (I will change the names throughout as...
4
9044
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have tried to use UTL_FILE package, but it operates on files consisting out of "line" of size no more than 32767 bytes and adds new_line symbol upon closing files. Also we checked out interMedia types and packages for writing BLOB to disk but now we get stuck with some strange error messages.
3
2605
by: Trevor.Dhu | last post by:
I am just starting to use python, and as such my question may well be a bit simplistic. I am currently trying to write what should be a very basic set of routines for reading and writing to a binary grid format (ERMapper). However: I am struggling to find the correct tools to write a row of a 2-dimensional array to a binary file. In matlab the command looks like: fid=fopen(filename,'wb');
13
2791
by: Leonardo Francalanci | last post by:
With mysql I know how much space a row will take, based on the datatype of it columns. I also (approximately) know the size of indexes. Is there a way to know that in postgresql? Is there a way to pack (compress) data, as with myisampack for mysql? Thank you
1
5903
by: stroumf | last post by:
Hi, Simple question, I want to receive compressed data from a server using AJAX. At the client I make an xmlHttpRequest. Next I want to set the accept-header to Gzip, but I keep getting errors on that under opera. At server-side I first check the accept-header, if it supports gzip I make a ZIP-stream and set the content-encoding to gzip. Normaly I would think the browser itself can handle it and give the uncompressed response.
19
4770
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
6
6991
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the supernode. Everything I have written works fine sending and receiving uncompressed data. But now I want to implement compression using the deflate algorithm as the Gnutella protocol accepts: Accept-Encoding: deflate Content-Encoding: deflate in the...
3
1592
by: Amit Kumar Saha | last post by:
Hello, I have a Python class with data members, say: class Foo: def __init__(self): self.a=7 self.b={1,3,4} #couple of more lists, tuples, etc
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.