Results 1 to 3 of 3

Thread: GZipStream zip file password protect

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2004
    Location
    San Isidro
    Posts
    326

    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
    I want to learn more
    grace

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: GZipStream zip file password protect

    You try using [SharpZipLib] for this.
    thanks
    amrita

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width