How do i decompress a String with GZip? - vb.net

The compression works just fine, and decompression also, but what should i do if the application got closed directly after the String got compressed? How can i decompress it having the String only?
//Compress
Dim mem As New IO.MemoryStream
Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
Dim sw As New IO.StreamWriter(gz)
sw.WriteLine("hello compression")
sw.Close()
Dim compressed As String = Convert.ToBase64String(mem.ToArray())
//Decompress
Dim mem2 As New IO.MemoryStream(mem.ToArray)
gz = New System.IO.Compression.GZipStream(mem2, IO.Compression.CompressionMode.Decompress)
Dim sr As New IO.StreamReader(gz)
MsgBox(sr.ReadLine)
sr.Close()
Dim decompressed As String = sr.ReadLine()

When the program is closed, the data in your memory stream is lost and not recoverable. You'll need to save the data to a file first.

Related

VB.net Delete a File after Creating Byte

When I try to delete a Tiff file after creating a byte I get an error stating that I cannot access file because it is being accessed by another process. I am Printing the Byte after the fact if that matters.
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(strFile)
Dim imageConverter As New ImageConverter()
Dim Bytes As Byte() = DirectCast(imageConverter.ConvertTo(image, GetType(Byte())), Byte())
Dim PageSettings As New PageSettings
Dim FS As New FileStream("C:\clean\Packing\output.TIF", FileMode.Create)
FS.Write(Bytes, 0, Bytes.Length)
FS.Close()
FS.Dispose()
IO.File.Delete(strFile)
Try to implement the Using Statement to dispose the object after you finish using it.
The End Using statement disposes of the resources under the Using block's control.
Using image = System.Drawing.Image.FromFile(strFile)
Dim imageConverter As New ImageConverter()
Dim Bytes As Byte() = DirectCast(imageConverter.ConvertTo(image, GetType(Byte())), Byte())
Dim pageSettings = New PageSettings
Using FS = New FileStream("C:\clean\Packing\output.TIF", FileMode.Create)
FS.Write(Bytes, 0, Bytes.Length)
End Using
End Using
IO.File.Delete(strFile)

vb.net downloading file using WebClient always corrupted file

I'm trying to download a ZIP file from Dropbox but every time I try it stops and only downloads ~100 kb of the file. It seems to corrupt any file I download from it, but however if I try to download using a normal browser it works.
Dim remoteUri As String = "https://www.dropbox.com/-/-------/test.zip?dl=0"
Dim fileName As String = "test.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = remoteUri + fileName
myWebClient.DownloadFile(myStringWebResource, fileName)
Your remoteUri is wrong. You're adding the filename twice. That gives you a bad url.
Dim remoteUri As String = "https://www.dropbox.com/-/-------/{0}?dl=1"
Dim fileName As String = "test.zip"
Dim myStringWebResource As String = Nothing
Dim myWebClient As New WebClient()
myStringWebResource = String.Format(remoteUri, fileName)
myWebClient.DownloadFile(myStringWebResource, fileName)
Try adding dl=1 to force download.

Writing FileStream to Local Path and not the server

I have the following code working successfully:
Protected Sub ExportExcel_Click(sender As Object, e As EventArgs) Handles ExportExcel.Click
Dim warnings As Warning()
Dim streamids As String()
Dim mimeType As String
Dim encoding As String
Dim filenameExtension As String
Dim fileName As String = "D:\Report" & DateTime.Now.ToString("yyyyMMdd_HHmmss") & ".xls"
Dim bytes As Byte() = ReportViewer1.LocalReport.Render("Excel", Nothing, mimeType, encoding, filenameExtension, streamids, warnings)
Using fs As New FileStream(fileName, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
End Using
lblMessage.Text = Functions.GetMessageConfirm("Report downloaded successfully in your D:/ at: " & Now.ToString)
End Sub
This code saves the file in the web server. I want to save the file on the client machine.
You're halfway there, probably. You can't just save files on the client anyway. Clients are webbrowsers, and they run JavaScript.
What you can do is use the download functionality of webbrowsers, to let them download the file which you just created. To do so, put the output on the server in a directory from where it can be downloaded, then return the new URL to the client.

VB.NET compress decompress string and return in string format

I need a vb.net function for compressing/decompressing a string, and return the result as a string. I found these two functions:
//Compress
Dim mem As New IO.MemoryStream
Dim gz As New System.IO.Compression.GZipStream(mem, IO.Compression.CompressionMode.Compress)
Dim sw As New IO.StreamWriter(gz)
sw.WriteLine("hello compression")
sw.Close()
//Decompress
Dim mem2 As New IO.MemoryStream(mem.ToArray)
gz = New System.IO.Compression.GZipStream(mem2, IO.Compression.CompressionMode.Decompress)
Dim sr As New IO.StreamReader(gz)
MsgBox(sr.ReadLine)
sr.Close()
But how do I make them return the compressed string as a string? thanks.
You would need to Base64 encode the compressed byte array to get a string representation.
Dim compressed As String = Convert.ToBase64String(mem.ToArray())
The decompressed string can just be read from the StreamReader.
Dim decompressed As String = sr.ReadLine()

How do I reload the formatted text into RTB?

I have a RTB in VB.NET, and put an event handler to save the formatted text into a file after encrypting it. However, I can't figure out how to reload the formatting - when I open it, it shows the symbols of formatting instead of formatted text. Here's my code:
Dim FileName As String = TextBox1.Text
File.Delete(FileName)
Dim EncryptElement As New TripleDESCryptoServiceProvider
EncryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)} '128 bit Key
EncryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)} ' 64 bit Initialization Vector
Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateEncryptor(EncryptElement.Key, EncryptElement.IV), CryptoStreamMode.Write)
Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)
sWriter.Close()
cStream.Close()
fStream.Close()
The above code is for saving, and the below code is for opening.
Dim FileName As String = TextBox1.Text
Dim DecryptElement As New TripleDESCryptoServiceProvider
DecryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)}
DecryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)}
Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateDecryptor(DecryptElement.Key, DecryptElement.IV), CryptoStreamMode.Read)
Dim sReader As New StreamReader(cStream)
Dim DecryptedData As String = ""
DecryptedData = sReader.ReadToEnd
RichTextBox1.AppendText(DecryptedData)
RichTextBox1.Enabled = True
Button1.Text = "OK"
sReader.Close()
cStream.Close()
fStream.Close()
Where is the problem?
You need RichTextBox1.SaveFile(SomeStream, RichTextBoxStreamType) I think StreamWriter is stuffing it up.
Oh and seeing as you just gave the world the keys for your encryption you might want to come up with a new one...
Added after comment not proven though.
Replace these two lines in your save routine
Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)
with
RichTextBox1.SaveFile(cStream,RichTextBoxStreamType.RichText);
and get rid of swriter.Close()
I think.