473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DeflateStream

2 New Member
Hi,

Can anybody tell me how to use the System.IO.Compr ession

Deflatestream Class.

I have a byte[] which is a compressed set of bytes that form a block.

I want to inflate the byte[] and return the new inflated byte[].

public static byte[] Deflate(byte[] c_block)
{
MemoryStream ms = new MemoryStream(c_ block);
DeflateStream zlib = new DeflateStream (ms,Compression Mode.Decompress );
zlib.Read(d_blo ck,0,??);
// Close the stream.
zlib.Close();
ms.Close();
return d_block;
}

Is this the right approach ?? If so, how can I tell the size of the inflated array d_block for the zlib.Read method. Can somebody provide some guidance.

Thanks

Phill
Jan 16 '07 #1
2 3583
AricC
1,892 Recognized Expert Top Contributor
From MSDN

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.IO
  3. Imports System.IO.Compression
  4.  
  5.  
  6.  
  7. Public Class DeflateTest
  8.     Shared msg As String
  9.    Public Shared Function ReadAllBytesFromStream(stream As Stream, buffer() As Byte) As Integer
  10.       ' Use this method is used to read all bytes from a stream.
  11.       Dim offset As Integer = 0
  12.       Dim totalCount As Integer = 0
  13.       While True
  14.          Dim bytesRead As Integer = stream.Read(buffer, offset, 100)
  15.          If bytesRead = 0 Then
  16.             Exit While
  17.          End If
  18.          offset += bytesRead
  19.          totalCount += bytesRead
  20.       End While
  21.       Return totalCount
  22.    End Function 'ReadAllBytesFromStream
  23.  
  24.  
  25.    Public Shared Function CompareData(buf1() As Byte, len1 As Integer, buf2() As Byte, len2 As Integer) As Boolean
  26.       ' Use this method to compare data from two different buffers.
  27.         If len1 <> len2 Then
  28.             msg = "Number of bytes in two buffer are different" & len1 & ":" & len2
  29.             MsgBox(msg)
  30.             Return False
  31.         End If
  32.  
  33.       Dim i As Integer
  34.       For i = 0 To len1 - 1
  35.          If buf1(i) <> buf2(i) Then
  36.                 msg = "byte " & i & " is different " & buf1(i) & "|" & buf2(i)
  37.                 MsgBox(msg)
  38.                 Return False
  39.          End If
  40.         Next i
  41.         msg = "All bytes compare."
  42.         MsgBox(msg)
  43.         Return True
  44.    End Function 'CompareData
  45.  
  46.  
  47.    Public Shared Sub DeflateCompressDecompress(filename As String)
  48.         msg = "Test compression and decompression on file " & filename
  49.         MsgBox(msg)
  50.  
  51.         Dim infile As FileStream
  52.       Try
  53.          ' Open the file as a FileStream object.
  54.          infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
  55.             Dim buffer(infile.Length - 1) As Byte
  56.          ' Read the file to ensure it is readable.
  57.             Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
  58.          If count <> buffer.Length Then
  59.                 infile.Close()
  60.                 msg = "Test Failed: Unable to read data from file"
  61.                 MsgBox(msg)
  62.                 Return
  63.          End If
  64.          infile.Close()
  65.          Dim ms As New MemoryStream()
  66.          ' Use the newly created memory stream for the compressed data.
  67.          Dim compressedzipStream As New DeflateStream(ms, CompressionMode.Compress, True)
  68.          compressedzipStream.Write(buffer, 0, buffer.Length)
  69.          ' Close the stream.
  70.             compressedzipStream.Close()
  71.  
  72.             msg = "Original size: " & buffer.Length & ", Compressed size: " & ms.Length
  73.             MsgBox(msg)
  74.  
  75.          ' Reset the memory stream position to begin decompression.
  76.          ms.Position = 0
  77.          Dim zipStream As New DeflateStream(ms, CompressionMode.Decompress)
  78.          Dim decompressedBuffer(buffer.Length + 100) As Byte
  79.          ' Use the ReadAllBytesFromStream to read the stream.
  80.          Dim totalCount As Integer = DeflateTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
  81.             msg = "Decompressed " & totalCount & " bytes"
  82.             MsgBox(msg)
  83.  
  84.          If Not DeflateTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) Then
  85.                 msg = "Error. The two buffers did not compare."
  86.                 MsgBox(msg)
  87.  
  88.          End If
  89.          zipStream.Close()
  90.         Catch e As Exception
  91.             msg = "Error: The file being read contains invalid data."
  92.             MsgBox(msg)
  93.         End Try
  94.  
  95.    End Sub 'DeflateCompressDecompress
  96.  
  97.     Public Shared Sub Main(ByVal args() As String)
  98.         Dim usageText As String = "Usage: DeflateTest <inputfilename>"
  99.         'If no file name is specified, write usage text.
  100.         If args.Length = 0 Then
  101.             Console.WriteLine(usageText)
  102.         Else
  103.             If File.Exists(args(0)) Then
  104.                 DeflateCompressDecompress(args(0))
  105.             End If
  106.         End If
  107.     End Sub 'Main
  108. End Class 'DeflateTest
  109.  
Jan 16 '07 #2
Flpit
2 New Member
Yep, tried that, but it relies on having the original file, compressing it, then uncompressing it, so it has a reference to the uncompressed file size.

Can you explain how to implement this in stimple terms ??
Jan 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
885
by: Neo | last post by:
I was changing over my code from the #Ziplib (http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx) compression streams to the new DefalteStream in the 2.0 framework. I noticed all my compressed data growing by 2x. Is there something that I am doing wrong? It would be sad to think that the new standard compression stream was written with a sub par algorithm. Any body have any experience with this?
10
17848
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing." Source="System.Xml" The code:
2
4504
by: Brent Rogers | last post by:
I am having trouble with the DeflateStream.Read() method. For some reason it wants to return Zero. I have this set of classes: =================== using System; using System.Collections.Generic; using System.Text; using System.IO; using System.IO.Compression;
1
2336
by: bthetford | last post by:
I am trying to roll a simple compression scheme for network communication in an app I am developing. On the sender side, I compress each block of a set amount of bytes individually, then send it over the NetworkStream along with the size of the original data, the size of the compressed data, and a byte telling the receiving end whether to expect more. On the receiving end, these blocks are received along with that size data and buffers...
4
4189
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
The compression part of this code works fine for me, but the decompression part of this code does not. The output file from that is the same as the compressed file. What is wrong with my code? Thanks, Jon 'compression starts here Dim src As FileStream = File.OpenRead(Fn) Dim dst As FileStream = File.Create(Ofn) Dim cmp As New DeflateStream(dst, CompressionMode.Compress)
1
10208
by: sedwick | last post by:
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compression classes GZipStream and DeflateStream, and I found it interesting to note that they'll create different-sized compressed files depending on the procedure you use to take in the source file's data. With one, almost every file I feed them for compression ends up significantly bigger. That code is below, almost verbatim...
1
2615
by: Smithers | last post by:
IIRC, we can use only System.IO.Compression.InflateStream to uncompress files compressed with System.IO Compression.DeflateStream. If that is in fact true, what are some alternatives you might recommend? I'm writing a Console app that, amongst other things compresses files. I want to be able to uncompress the files using WinZip and other similar and generally available compression/uncompression utilities. I have seen and used 7-Zip,...
4
4499
by: Grok | last post by:
Trying to zip/unzip files from VB.NET 2005 with 2.0 .NET Framework ... but the file created "SampleFile.zip" is not a readable zip file. (Using GZipStream works correctly to produce a readable .gz file) How do I properly create a .zip file without 3rd-party lib? Dim sourceFile As FileStream = File.OpenRead("c:\temp\SampleFile.chm") Dim destFile As FileStream = File.Create("c:\temp\SampleFile.zip")
2
2270
by: Author | last post by:
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...
0
9589
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
9423
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,...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3959
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
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.