Click here to Skip to main content
15,881,791 members
Articles / Desktop Programming / Windows Forms

ZipTrack

Rate me:
Please Sign up or sign in to vote.
4.86/5 (28 votes)
2 Dec 2009CPOL5 min read 58.9K   781   78  
A comprehensive way to manage all of your downloaded zip files.

Imports System
Imports System.IO

Imports vb = Microsoft.VisualBasic
Imports ZipTrack.Gui.Components
Imports ICSharpCode.SharpZipLib.Zip

Friend NotInheritable Class ArchiveFactory

    Friend Shared Event BeginBuilding As EventHandler(Of System.EventArgs)

    Friend Shared Event BuildComplete As EventHandler(Of EventArgs.DocumentEventArgs)

    Friend Shared Function Create(ByVal e As EventArgs.WebsiteEventArgs) As Boolean
        Dim WebsiteID As Integer = e.Website.WebsiteID
        Dim mFileUrl As String = String.Empty

        If WebsiteID <= 0 Then
            MessageBox.Show("To begin archive indexing, you must select a Website from the list.", "Missing Website information", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If

        Try
            Dim ofd As New FileFolderDialog
            With ofd
                .Dialog.Title = "Browse file directory"
                .Dialog.Filter = "All Files (*.zip)|*.zip"
                .Dialog.Multiselect = False

                .Dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

                If .ShowDialog = DialogResult.OK Then
                    mFileUrl = .Dialog.FileName

                    Dim FileSize As Integer = 1
                    Dim Comment As String = "n/a"
                    Dim zip As ICSharpCode.SharpZipLib.Zip.ZipFile = Nothing

                    Dim nfo As New FileInfo(mFileUrl)
                    Using fstream As New FileStream(nfo.FullName, FileMode.Open)
                        zip = New ZipFile(fstream)
                        FileSize = CInt(zip.Count)
                        Comment = zip.ZipFileComment
                    End Using

                    Dim biz As New Business.Archive
                    With biz
                        .Datestamp = nfo.CreationTime
                        .FileUrl = nfo.FullName
                        .Name = Path.GetFileNameWithoutExtension(nfo.Name)
                        .WebsiteID = WebsiteID
                        .FileSize = FileSize
                        .Comment = Comment
                    End With

                    ' Insert the Archive into our database
                    biz.ArchiveID = ArchiveManager.Insert(biz)
                End If

            End With

            Return True
        Catch ex As Exception

        End Try
        Return False
    End Function


#Region "Rebuild"
    Friend Shared Sub Rebuild(ByVal e As EventArgs.ArchiveEventArgs)
        Try
            Dim FileUrl As String = e.Archive.FileUrl

            If File.Exists(FileUrl) Then
                Dim zip As ICSharpCode.SharpZipLib.Zip.ZipFile = Nothing
                Dim biz As Business.Document = Nothing

                Using fstream As New FileStream(FileUrl, FileMode.Open)
                    zip = New ZipFile(fstream)
                    Dim nCount As Integer = CInt(zip.Count)

                    For n As Integer = 0 To nCount - 1
                        Dim mEntry As ZipEntry = zip.EntryByIndex(n)
                        Dim FileName As String = mEntry.Name

                        ' Ensure that we have captured a 'real' file
                        If Not String.Compare(vb.Right(FileName, 1), "/") = 0 Then
                            Dim ContentType As ContentType = Interpreter.GetContentType(mEntry.Name)
                            Dim LanguageType As Integer = Interpreter.GetLanguageType(mEntry.Name)

                            Dim Comment As String = "n/a"
                            Dim Content As String = "Unreadable content type"
                            Dim FileSize As Integer = CInt(mEntry.Size)
                            Dim Percentage As Integer = 100

                            ' Create a new document
                            biz = New Business.Document With {.ArchiveID = e.Archive.ArchiveID}
                            RaiseEvent BeginBuilding(Nothing, System.EventArgs.Empty)

                            If FileSize > 0 Then
                                Percentage = CInt(CDbl(mEntry.CompressedSize / mEntry.Size) * 100)
                            End If

                            If Not IsNothing(mEntry.Comment) Then Comment = mEntry.Comment

                            ' only want to read text type files
                            If ContentType = ContentType.SourceCode OrElse ContentType = ContentType.ASCIIFile Then
                                Using sr As New StreamReader(zip.GetInputStream(mEntry))

                                    ' Clean the string of those nasty quote marks
                                    Dim value As String = Replace(sr.ReadToEnd, "'", "’")
                                    Content = String.Format("{0}", value)

                                End Using
                            End If

                            With biz
                                .ContentTypeID = ContentType
                                .Content = Content
                                .LanguageID = LanguageType
                                .Name = FileName
                                .Version = mEntry.Version
                                .Percentage = Percentage
                                .FileDate = mEntry.DateTime
                                .FileSize = FileSize
                                .Comment = Comment
                            End With

                            ' Insert this document to our collection
                            biz.DocumentID = DocumentManager.Insert(biz)
                        End If

                        RaiseEvent BuildComplete(Nothing, New EventArgs.DocumentEventArgs(biz))
                    Next n
                End Using

                Application.DoEvents()

            End If
        Catch ex As Exception

        End Try
    End Sub
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Arkitech EBC Corporation
United States United States
MS, BBA, software developer, consultant, and trainer. Specializing in building data-centric applications designed for business, university, community & faith based organizations. Started developing Excel VBA macros and never looked back. Freelance developer utilizing VB.Net, SQL Server, Microsoft Access, and ASP.Net.

Comments and Discussions