7 Replies - 1761 Views - Last Post: 26 February 2009 - 01:10 PM Rate Topic: -----

#1 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Compression

Posted 26 February 2009 - 11:26 AM

how would i commpress a folder from a text box becasue what I have at the moment is just returning a value of true


Dim compres As IO.Compression.CompressionMode
		TextBox1.Text = IO.Compression.CompressionMode.Compress = IO.Compression.CompressionMode.Compress = 1



And I dont understand how to use the compression :crazy:

(This is the complete code I cant work this out lol)

Is This A Good Question/Topic? 0
  • +

Replies To: Compression

#2 Ryan Marfone  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 23-February 09

Re: Compression

Posted 26 February 2009 - 11:43 AM

As with any IO manipulation you need to create a stream to access the data.

Fortunately in .Net they have included classes to compress for you. I believe the class you should look into is called ZipOutputStream.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Compression

Posted 26 February 2009 - 11:48 AM

You're getting true because you're just referencing the CompressionMode Enumeration. Here's an example of using the GZipStream Class to compress a file

Public Function CompressFile(ByRef file As String, ByRef destination As String) As Boolean
	Dim source() As Byte
	Dim compressed() As Byte

	' Validate paths
	If IO.File.Exists(file) = False Then
		MessageBox.Show("Please specify the valid file path to compress")
		Return False
		Exit Function
	Else
		If IO.Directory.Exists(destination) = False Then
			MessageBox.Show("Please specify the valid path of destination folder")
			Return False
			Exit Function
		End If
	End If

	Try
		Dim name As String = file.Substring(file.LastIndexOf("\") + 1)

		source = System.IO.File.ReadAllBytes(file)
		compressed = Compress(source)

		System.IO.File.WriteAllBytes(destination & "\" & name & ".zip", compressed)

		MessageBox.Show("Compression Successful!")
		Return True
	Catch ex As Exception
		MessageBox.Show("Compression Error: " & ex.ToString())
	End Try
End Function

Public Function Compress(ByVal byteSource() As Byte) As Byte()
	' Create a GZipStream object and memory stream object to store compressed stream
	Dim memoryStream As New MemoryStream()
	Dim gZipStream As New GZipStream(memoryStream, CompressionMode.Compress, True)
	gZipStream.Write(byteSource, 0, byteSource.Length)
	gZipStream.Dispose()
	memoryStream.Position = 0
	' Write compressed memory stream into byte array
	Dim buffer(memoryStream.Length) As Byte
	memoryStream.Read(buffer, 0, buffer.Length)
	memoryStream.Dispose()
	Return buffer
End Function



Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#4 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Compression

Posted 26 February 2009 - 12:00 PM

How would i Call this function? liek in a button because I treid calling compress but it wont accept it
Edit - I also tried The compressfile one as well but it doesnt seem to work... :crazy:

This post has been edited by Asscotte: 26 February 2009 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Compression

Posted 26 February 2009 - 12:06 PM

Here's the code returning a string instead of a Boolean

Public Function CompressFile(ByRef file As String, ByRef destination As String) As String
	Dim source() As Byte
	Dim compressed() As Byte
	Dim message as String = string.Empty
	' Validate paths
	If IO.File.Exists(file) = False Then
		message "Please specify the valid file path to compress"
		Exit Function
	Else
		If IO.Directory.Exists(destination) = False Then
			message "Please specify the valid path of destination folder"
			Exit Function
		End If
	End If

	Try
		Dim name As String = file.Substring(file.LastIndexOf("\") + 1)

		source = System.IO.File.ReadAllBytes(file)
		compressed = Compress(source)

		System.IO.File.WriteAllBytes(destination & "\" & name & ".zip", compressed)

		message "Compression Successful!"
	Catch ex As Exception
		message "Compression Error: " & ex.ToString()
	End Try
	return message
End Function

Public Function Compress(ByVal byteSource() As Byte) As Byte()
	' Create a GZipStream object and memory stream object to store compressed stream
	Dim memoryStream As New MemoryStream()
	Dim gZipStream As New GZipStream(memoryStream, CompressionMode.Compress, True)
	gZipStream.Write(byteSource, 0, byteSource.Length)
	gZipStream.Dispose()
	memoryStream.Position = 0
	' Write compressed memory stream into byte array
	Dim buffer(memoryStream.Length) As Byte
	memoryStream.Read(buffer, 0, buffer.Length)
	memoryStream.Dispose()
	Return buffer
End Function



Then in your buttons click event you can do something like this
MessageBox.Show(CompressFile(TextBox1.Text, TextBox2.Text)



TextBox1 is where the file name is at, TextBox2 is where the destination folder is at
Was This Post Helpful? 0
  • +
  • -

#6 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Compression

Posted 26 February 2009 - 12:11 PM

Thanks But now I am getting this error on the word "message" - expression is not a method
I know its a message box but how would I "dim It in" (loose terminolagy) :)
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Compression

Posted 26 February 2009 - 12:54 PM

Sorry about that, it's supposed to be message =. Forgot the equals sign
Was This Post Helpful? 0
  • +
  • -

#8 Asscotte  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: Compression

Posted 26 February 2009 - 01:10 PM

Thats Okay everybody does it though for me its normally the brackets or commas lol :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1