Någon har en kodsnutt eller länk till info om hur man kan komprimera och packa upp t ex en sträng (dvs ej en hel fil som zip). SharpZipLib klarar att komprimera strömmar i minnet om jag inte minns fel. Här är en snutt kod (inte så snygg, men den funkar) som utför en LZW-komprimering (samma teknik som i GIF-bilder). LZW är fri att använda i ickekomersiella syften.Komprimera en sträng
/TommySv: Komprimera en sträng
http://www.icsharpcode.net/OpenSource/SharpZipLib/default.asp
MSSv: Komprimera en sträng
Intressant...
Men vill ni förklara varför man vill göra det??
Undrar freddaSv: Komprimera en sträng
<code>
Public Class clsPack
Private sDictionary() As String = Nothing
Private Function DictionaryMatch(ByVal s As String) As Integer
DictionaryMatch = 0
Dim i As Integer
For i = 0 To Me.sDictionary.Length - 1
If sDictionary(i) = s Then
DictionaryMatch = i
Exit Function
End If
Next i
End Function
Private Sub AddToDictionary(ByVal s As String, ByVal Position As Integer)
If Position > 4096 Then
ReDim Preserve sDictionary(Position)
End If
sDictionary(Position) = s
End Sub
Public Function Pack(ByVal UnpackedString As String) As Array
If UnpackedString.Length = 0 Then
Return Nothing
End If
Dim sWorkingWord As String = Nothing
Dim sCurrentChar As String = Nothing
Dim aResult() As Integer
Dim nDictionaryCounter As Integer
Dim i As Integer
Dim j As Integer
ReDim sDictionary(4096)
For nDictionaryCounter = 0 To 255
sDictionary(nDictionaryCounter) = Chr(nDictionaryCounter)
Next nDictionaryCounter
For i = 1 To UnpackedString.Length
sCurrentChar = Mid(UnpackedString, i, 1)
If DictionaryMatch(sWorkingWord & sCurrentChar) Then
sWorkingWord = sWorkingWord & sCurrentChar
Else
ReDim Preserve aResult(j)
aResult(j) = DictionaryMatch(sWorkingWord)
j = j + 1
AddToDictionary(sWorkingWord & sCurrentChar, nDictionaryCounter)
nDictionaryCounter = nDictionaryCounter + 1
sWorkingWord = sCurrentChar
End If
Next i
ReDim Preserve aResult(j)
aResult(j) = DictionaryMatch(sWorkingWord)
Return aResult
End Function
Public Function UnPack(ByVal PackedString As Array) As String
Dim i As Integer
Dim sWorkingWord As String = Nothing
Dim sCurrentChar As String = Nothing
Dim sResult As String = Nothing
Dim sLastWord As String
Dim sStack As String
Dim nDictionaryCounter As Integer
ReDim sDictionary(4096)
For nDictionaryCounter = 0 To 255
sDictionary(nDictionaryCounter) = Chr(nDictionaryCounter)
Next nDictionaryCounter
sWorkingWord = sDictionary(PackedString(0))
sResult = sResult & sWorkingWord
For i = 1 To PackedString.Length - 1
sLastWord = sWorkingWord
sWorkingWord = sDictionary(PackedString(i))
If DictionaryMatch(sWorkingWord) Then
sResult = sResult & sWorkingWord
sStack = sLastWord
sCurrentChar = Microsoft.VisualBasic.Left(sWorkingWord, 1)
AddToDictionary(sStack & sCurrentChar, nDictionaryCounter)
nDictionaryCounter = nDictionaryCounter + 1
Else
sStack = sLastWord
sCurrentChar = Microsoft.VisualBasic.Left(sLastWord, 1)
sResult = sResult & sStack & sCurrentChar
AddToDictionary(sStack & sCurrentChar, nDictionaryCounter)
nDictionaryCounter = nDictionaryCounter + 1
End If
Next i
Return sResult
End Function
End Class
</code>