CSharpTest.Net
Compress(String) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.IO Namespace > IOStream Class > Compress Method : Compress(String) Method

filename

Glossary Item Box

Compress the existing file using an in-place replace.

Syntax

Visual Basic (Declaration) 
Public Overloads Shared Sub Compress( _
   ByVal filename As String _
) 
C# 
public static void Compress( 
   string filename
)

Parameters

filename

Example

Library/Library.Test/TestIOStreamUtils.cs

C#Copy Code
byte[] testData = new byte[ushort.MaxValue];
new Random().NextBytes(testData);
Array.Clear(testData, 0, testData.Length / 2);
Hash compressedHash;
Hash hash = Hash.SHA512(testData);

using (TempFile f = new TempFile())
{
    f.WriteAllBytes(testData);
    Assert.AreEqual((long)testData.Length, f.Length);

    IOStream.Compress(f.TempPath);
    Assert.IsTrue(f.Length < testData.Length);
    compressedHash = Hash.SHA512(f.Read());
    Assert.IsFalse(compressedHash == hash);

    IOStream.Decompress(f.TempPath);
    Assert.AreEqual((long)testData.Length, f.Length);
    Assert.IsTrue(hash == Hash.SHA512(f.Read()));
}

MemoryStream ms = new MemoryStream(testData), gz = new MemoryStream();
IOStream.Compress(ms, gz);

Assert.IsTrue(compressedHash == Hash.SHA512(gz.ToArray()));
ms = new MemoryStream();
gz.Position = 0;
IOStream.Decompress(gz, ms);

Assert.IsTrue(hash == Hash.SHA512(ms.ToArray()));
VB.NETCopy Code
Dim testData As Byte() = New Byte(UShort.MaxValue) {}
New Random().NextBytes(testData)
Array.Clear(testData, 0, testData.Length / 2)
Dim compressedHash As Hash
Dim hash As Hash = Hash.SHA512(testData)

Using f As New TempFile()
    f.WriteAllBytes(testData)
    Assert.AreEqual(DirectCast(testData.Length, Long), f.Length)

    IOStream.Compress(f.TempPath)
    Assert.IsTrue(f.Length < testData.Length)
    compressedHash = Hash.SHA512(f.Read())
    Assert.IsFalse(compressedHash = hash)

    IOStream.Decompress(f.TempPath)
    Assert.AreEqual(DirectCast(testData.Length, Long), f.Length)
    Assert.IsTrue(hash = Hash.SHA512(f.Read()))
End Using

Dim ms As New MemoryStream(testData), gz As New MemoryStream()
IOStream.Compress(ms, gz)

Assert.IsTrue(compressedHash = Hash.SHA512(gz.ToArray()))
ms = New MemoryStream()
gz.Position = 0
IOStream.Decompress(gz, ms)

Assert.IsTrue(hash = Hash.SHA512(ms.ToArray()))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys