GZipStream zip file password protect
How can i password protect my zip file?
i have the code below i got from the net but the problem is no code to protect file the file
Code:
Public Function CompressFile(ByRef file As String, ByRef destination As String) As String
'Make sure user provided a valid file with path
If IO.File.Exists(file) = False Then
Return "Please specify a valid file (and path) to compress"
Exit Function
Else
'Make sure the destination directory exists
If IO.Directory.Exists(destination) = False Then
Return "Please provide a destination location"
Exit Function
End If
End If
Try
'Get just the name of the file
Dim name As String = Path.GetFileName(file)
'Convert the file to a byte array
Dim source() As Byte = System.IO.File.ReadAllBytes(file)
'Now we need to compress the byte array
Dim compressed() As Byte = ConvertToByteArray(source)
'Write the new file in the destination directory
System.IO.File.WriteAllBytes(destination & "\" & name & ".zip", compressed)
Return "Compression Successful!"
Catch ex As Exception
Return "Compression Error: " & ex.ToString()
End Try
End Function
Public Function ConvertToByteArray(ByVal source() As Byte) As Byte()
'Create a MemoryStrea
Dim memoryStream As New MemoryStream()
'Create a new GZipStream for holding the file bytes
Dim gZipStream As New GZipStream(memoryStream, CompressionMode.Compress, True)
'Write the bytes to the stream
gZipStream.Write(source, 0, source.Length)
gZipStream.Dispose()
memoryStream.Position = 0
'Create a byte array to act as our buffer
Dim buffer(memoryStream.Length) As Byte
'read the stream
memoryStream.Read(buffer, 0, buffer.Length)
'CLose & clean up
memoryStream.Dispose()
'Return the byte array
Return buffer
End Function
Re: GZipStream zip file password protect
You try using [SharpZipLib] for this.
Re: GZipStream zip file password protect
I like DotNetZip: http://dotnetzip.codeplex.com/
Code:
Using zip As New ZipFile()
With zip
.AddFile("C:\Temp\states.txt")
.Password = "ItsASecret"
.Save("C:\Temp\TheZipFile.zip")
End With
End Using