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
2 3583 AricC 1,892
Recognized Expert Top Contributor
From MSDN -
Imports System
-
Imports System.IO
-
Imports System.IO.Compression
-
-
-
-
Public Class DeflateTest
-
Shared msg As String
-
Public Shared Function ReadAllBytesFromStream(stream As Stream, buffer() As Byte) As Integer
-
' Use this method is used to read all bytes from a stream.
-
Dim offset As Integer = 0
-
Dim totalCount As Integer = 0
-
While True
-
Dim bytesRead As Integer = stream.Read(buffer, offset, 100)
-
If bytesRead = 0 Then
-
Exit While
-
End If
-
offset += bytesRead
-
totalCount += bytesRead
-
End While
-
Return totalCount
-
End Function 'ReadAllBytesFromStream
-
-
-
Public Shared Function CompareData(buf1() As Byte, len1 As Integer, buf2() As Byte, len2 As Integer) As Boolean
-
' Use this method to compare data from two different buffers.
-
If len1 <> len2 Then
-
msg = "Number of bytes in two buffer are different" & len1 & ":" & len2
-
MsgBox(msg)
-
Return False
-
End If
-
-
Dim i As Integer
-
For i = 0 To len1 - 1
-
If buf1(i) <> buf2(i) Then
-
msg = "byte " & i & " is different " & buf1(i) & "|" & buf2(i)
-
MsgBox(msg)
-
Return False
-
End If
-
Next i
-
msg = "All bytes compare."
-
MsgBox(msg)
-
Return True
-
End Function 'CompareData
-
-
-
Public Shared Sub DeflateCompressDecompress(filename As String)
-
msg = "Test compression and decompression on file " & filename
-
MsgBox(msg)
-
-
Dim infile As FileStream
-
Try
-
' Open the file as a FileStream object.
-
infile = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
-
Dim buffer(infile.Length - 1) As Byte
-
' Read the file to ensure it is readable.
-
Dim count As Integer = infile.Read(buffer, 0, buffer.Length)
-
If count <> buffer.Length Then
-
infile.Close()
-
msg = "Test Failed: Unable to read data from file"
-
MsgBox(msg)
-
Return
-
End If
-
infile.Close()
-
Dim ms As New MemoryStream()
-
' Use the newly created memory stream for the compressed data.
-
Dim compressedzipStream As New DeflateStream(ms, CompressionMode.Compress, True)
-
compressedzipStream.Write(buffer, 0, buffer.Length)
-
' Close the stream.
-
compressedzipStream.Close()
-
-
msg = "Original size: " & buffer.Length & ", Compressed size: " & ms.Length
-
MsgBox(msg)
-
-
' Reset the memory stream position to begin decompression.
-
ms.Position = 0
-
Dim zipStream As New DeflateStream(ms, CompressionMode.Decompress)
-
Dim decompressedBuffer(buffer.Length + 100) As Byte
-
' Use the ReadAllBytesFromStream to read the stream.
-
Dim totalCount As Integer = DeflateTest.ReadAllBytesFromStream(zipStream, decompressedBuffer)
-
msg = "Decompressed " & totalCount & " bytes"
-
MsgBox(msg)
-
-
If Not DeflateTest.CompareData(buffer, buffer.Length, decompressedBuffer, totalCount) Then
-
msg = "Error. The two buffers did not compare."
-
MsgBox(msg)
-
-
End If
-
zipStream.Close()
-
Catch e As Exception
-
msg = "Error: The file being read contains invalid data."
-
MsgBox(msg)
-
End Try
-
-
End Sub 'DeflateCompressDecompress
-
-
Public Shared Sub Main(ByVal args() As String)
-
Dim usageText As String = "Usage: DeflateTest <inputfilename>"
-
'If no file name is specified, write usage text.
-
If args.Length = 0 Then
-
Console.WriteLine(usageText)
-
Else
-
If File.Exists(args(0)) Then
-
DeflateCompressDecompress(args(0))
-
End If
-
End If
-
End Sub 'Main
-
End Class 'DeflateTest
-
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 ??
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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:
|
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;
|
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...
|
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)
| |
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...
|
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,...
|
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")
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |