Julekalender 2004 om Whidbey


13. dec 2004 23:49

Version 2 af .NET frameworket understøtter ZIP kompression og dekompression. Så nu behøver man ikke længere ty til tredjeparts-komponenter eller brug af J#s klassebiblioteker, hvis man vil komprimere data.

I nedenstående eksempel komprimeres og efterfølgende dekomprimeres en streng ved hjælp af GZipStream.

Private Sub btnZipString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZipString.Click

  Dim inString As String = "GZipStream provides methods and properties used to compress and decompress streams."
  For n As Integer = 1 To 2
    inString += inString
  Next
  Dim inData As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(inString)

  Dim compressedData As Byte() = CompressData(inData)

  Dim outString As String = System.Text.ASCIIEncoding.ASCII.GetString(DeCompressData(compressedData))

  Dim message As String
  message = "Den originale streng er: " + vbCrLf + inString + vbCrLf + vbCrLf
  message += "Den originale streng fylder " + inString.Length.ToString() + " bytes." + vbCrLf
  message += "Den komprimerede streng fylder " + compressedData.Length.ToString() + "bytes." + vbCrLf
  message += "Den dekomprimerede streng fylder: " + outString.Length.ToString() + vbCrLf + vbCrLf
  message += "Den dekomprimerede streng er: " + vbCrLf + outString
  MessageBox.Show(message, "ZipDemo")

End Sub

Public Function CompressData(ByVal data As Byte()) As Byte()

  Dim ms As New System.IO.MemoryStream()

  Dim zip As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress)
  zip.Write(data, 0, data.Length)
  zip.Close()

  Dim outData As Byte() = ms.ToArray()

  ms.Close()

  Return outData

End Function

Public Function DeCompressData(ByVal data As Byte()) As Byte()

  Dim ms As New System.IO.MemoryStream(data)
  Dim outData As Byte()

  Dim zip As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress)
  outData = ReadAllBytesFromStream(zip)
  zip.Close()

  ms.Close()

  Return outData

End Function

Public Shared Function ReadAllBytesFromStream(ByVal stream As System.IO.Stream) As Byte()

  Const blockSize As Integer = 10000
  Dim buffer As Byte()
  ReDim Preserve buffer(-1)
  Dim offset As Integer = 0
  Dim totalCount As Integer = 0

  Do
    ReDim Preserve buffer(buffer.GetLongLength(0) - 1 + blockSize)
    Dim bytesRead As Integer = stream.Read(buffer, offset, blockSize)
    If bytesRead = 0 Then
      Exit Do
    End If
    offset += bytesRead
    totalCount += bytesRead
  Loop

  ReDim Preserve buffer(totalCount - 1)

  Return buffer

End Function
private void btnZipString_Click(object sender, System.EventArgs e)
{
  string inString = "GZipStream provides methods and properties used to compress and decompress streams.";
  for (int n = 1; n <= 2; n++)
  {
    inString += inString;
  }
  byte[] inData = System.Text.ASCIIEncoding.ASCII.GetBytes(inString);

  byte[] compressedData = CompressData(inData);

  string outString = System.Text.ASCIIEncoding.ASCII.GetString(DeCompressData(compressedData));

  string message = "";
  message = "Den originale streng er: \n" + inString + "\n\n";
  message += "Den originale streng fylder " + inString.Length.ToString() + " bytes." + "\n";
  message += "Den komprimerede streng fylder " + compressedData.Length.ToString() + " bytes." + "\n";
  message += "Den dekomprimerede streng fylder: " + outString.Length.ToString() + "\n\n";
  message += "Den dekomprimerede streng er: " + "\n" + outString;
  MessageBox.Show(message, "ZipDemo");
}

public byte[] CompressData(byte[] data)
{
  System.IO.MemoryStream ms = new System.IO.MemoryStream();

  System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);
  zip.Write(data, 0, data.Length);
  zip.Close();

  byte[] outData = ms.ToArray();
  ms.Close();

  return outData;
}

public byte[] DeCompressData(byte[] data)
{
  System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
  byte[] outData;
  System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
  outData = ReadAllBytesFromStream(zip);
  zip.Close();
  ms.Close();
  return outData;
}

public static byte[] ReadAllBytesFromStream(System.IO.Stream stream)
{
  const int blockSize = 10000;
  byte[] buffer = new byte[0];
  byte[] tempBuffer = new byte[blockSize];

  do
  {
    int bytesRead = stream.Read(tempBuffer, 0, blockSize);
    if (bytesRead == 0)
    {
      break;
    }
    buffer = AppendByteArrays(buffer, ByteArrayPart(tempBuffer, 0, bytesRead));
  } while (true);

  return buffer;
}

public static byte[] ByteArrayPart(byte[] array, long index, long length)
{
  byte[] resultArray = new byte[length];

  for (long n = index; n < index + length; n++)
  {
    resultArray[n-index] = array[n];
  }

  return resultArray;
}

public static byte[] AppendByteArrays(byte[] array1, byte[] array2)
{
  byte[] copyArray = new byte[array1.GetLongLength(0) + array2.GetLongLength(0)];
  array1.CopyTo(copyArray, 0);
  array2.CopyTo(copyArray, array1.GetLongLength(0));
  return copyArray;
}


Abonnér på mit RSS feed.   Læs også de øvrige indlæg i denne Blog.