Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Compress a String

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adriano - 11 Nov 2006 02:08 GMT
Can anyone recommend a simple way to compress/decomress a String in .NET 1.1 ?

I have a random string of 70 characters,  the output from a DES3 encryption, and I wish to reduce the lengh of it,

thanks,
Arne Vajhøj - 11 Nov 2006 02:58 GMT
> Can anyone recommend a simple way to compress/decomress a String in .NET
> 1.1 ?
>  
> I have a random string of 70 characters,  the output from a DES3
> encryption, and I wish to reduce the lengh of it,

For general compression look at something like #ZipLib.

But I don't think it is worth it in this case. Encrypted
data usually don't compress very well.

Arne
giddy - 11 Nov 2006 06:38 GMT
C# comes with the GZip and Deflate algorithm for compression
system.IO.Compression
...
..
..
//GZIP
static void SaveCompressedFile(string filename, string data)
{
FileStream fileStream =
new FileStream(filename, FileMode.Create, FileAccess.Write);
GZipStream compressionStream =
new GZipStream(fileStream, CompressionMode.Compress);
StreamWriter writer = new StreamWriter(compressionStream);
writer.Write(data);
writer.Close();
}

static string LoadCompressedFile(string filename)
{
FileStream fileStream =
new FileStream(filename, FileMode.Open, FileAccess.Read);
GZipStream compressionStream =
new GZipStream(fileStream, CompressionMode.Decompress);
StreamReader reader = new StreamReader(compressionStream);
string data = reader.ReadToEnd();
reader.Close();
return data;
}

//replacing GZipStream with DeflateStream uses the Deflate algorithm

but like he said.. encrypted data does'nt compress well becuase the
algorithms just removes repeats.... it works well with strings that
have repetitions.

I would suggest not encrypting the text....  compress them first (they
end up somewhat encrypted , but someone could just read and uncompress
it with the above code) ... so you can try encrypting the compressed
data.

Hope i helped,
Gideon

> > Can anyone recommend a simple way to compress/decomress a String in .NET
> > 1.1 ?
Arne Vajhøj - 11 Nov 2006 14:24 GMT
> C# comes with the GZip and Deflate algorithm for compression
> system.IO.Compression

.NET 2.x does !

And the original poster explicit stated .NET 1.1 ...

Arne
giddy - 12 Nov 2006 06:43 GMT
i'm sorry!! i did'nt see that!

Gideon
Arne Vajh?j wrote:
> And the original poster explicit stated .NET 1.1 ...
Jon Skeet [C# MVP] - 11 Nov 2006 07:26 GMT
> Can anyone recommend a simple way to compress/decomress a String in
> .NET 1.1 ?
>
> I have a random string of 70 characters, the output from a DES3
> encryption, and I wish to reduce the lengh of it,

I doubt very much whether you'll be able to compress it at all. The
more "random" data is, the less likely you are to be able to compress
it. Compression is almost always based on repeated patterns.

One thought though: the output of DES3 is binary, not text: how are you
converting the bytes into a string? There may be a way of doing that
which saves some more size.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Adriano - 13 Nov 2006 18:35 GMT
All,

Thanks for all your advice. Indeed DES3 output is not compressable, as it mostly noise,

cheers, Adrian

 Can anyone recommend a simple way to compress/decomress a String in .NET 1.1 ?

 I have a random string of 70 characters,  the output from a DES3 encryption, and I wish to reduce the lengh of it,

 thanks,

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.