Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Zipping a file
Message
De
02/02/2010 02:08:56
 
 
À
02/02/2010 00:12:39
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Versions des environnements
Environment:
VB 9.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01447051
Message ID:
01447057
Vues:
49
>From .NET, is there an existing class that would allow me to zip a file?

The following seems to work like a charm:
Imports System.IO

Namespace Framework

    Public Class Zip

        Public cDestinationFile As String = ""
        Public cSourceFolder As String = ""
        Public cError As String = ""
        Public cFile As String = ""
        Private oFile As Collection = New Collection()
        Private oApp As Framework.App
        Private oProcess As Framework.LXProcess

        ' This is when we access the data provider in desktop and Web service mode
        Public Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
        End Sub

        ' This is when we access the data provider in desktop and Web service mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
        End Sub

        ' Zip a file
        Public Function Zip() As Boolean
            Dim lcFile As String = ""
            Dim llOK As Boolean = False
            Dim loByte(21) As Byte
            Dim loObject As Object
            Dim loDestinationFolder As Shell32.Folder
            Dim loFile As Shell32.FolderItem
            Dim loShellClass As Shell32.ShellClass = New Shell32.ShellClass
            Dim loSourceFolder As Shell32.Folder

            Try

                ' If the source folder does not exist
                If Not oApp.DirectoryExist(cSourceFolder) Then
                    cError = "The directory " + cSourceFolder + " does not exist."
                    Exit Try
                End If

                loSourceFolder = loShellClass.NameSpace(cSourceFolder)

                loByte(0) = 80 : loByte(1) = 75 : loByte(2) = 5 : loByte(3) = 6

                ' Make an empty PKZip file
                File.WriteAllBytes(cDestinationFile, loByte)

                loDestinationFolder = loShellClass.NameSpace(cDestinationFile)

                ' For each file
                For Each loObject In oFile
                    lcFile = loObject(1)

                    ' If the file does not exist
                    If Not oApp.FileExist(cSourceFolder + "\" + lcFile) Then
                        cError = "The file " + cSourceFolder + "\" + lcFile + " does not exist."

                        ' Remove the temporary file
                        If Not oApp.DeleteFile(cDestinationFile) Then

                        End If

                        Exit Try
                    End If

                    loFile = loSourceFolder.Items.Item(lcFile)
                    loDestinationFolder.CopyHere(loFile, 20)
                Next

                llOK = True
            Catch loError As Exception
                cError = loError.Message
            End Try

            Return llOK
        End Function

        ' Add a file
        ' expC1 File name
        Public Function AddFile(ByVal tcFile As String) As Boolean
            Dim loObject(1) As Object

            loObject(1) = tcFile

            oFile.Add(loObject)
            Return True
        End Function

    End Class

End Namespace
To initiate:
        Dim loZip As Framework.Framework.Zip = New Framework.Framework.Zip(oApp)

        loZip.cSourceFolder = "d:\"
        loZip.AddFile("Server.docx")
        loZip.AddFile("Test.jpg")
        loZip.cDestinationFile = "d:\Test\Test.zip"
        If Not loZip.Zip() Then
            MessageBox.Show("Error: " + loZip.cError)
            Exit Sub
        End If
It makes use of the Shell to accomplish. This is a very fast and fully compliant with the shell extension as installed with WinZip.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform