WinZip,VB.Net命令行最大zip文件大小

时间:2015-02-26 10:45:55

标签: vb.net command-line filesize winzip

我需要有关WinZip命令行的帮助。 我目前有一个工作正常的命令行,但如果文件夹超过4GB则压缩。我有非常大的Tiffs文件,超过100MB。我希望在文件达到2GB时拆分文件。

这是我的代码

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        deleteCount = 0
        Dim i, x As Integer
        Dim reNamedTo As String
        Dim newFileURL, newFolderURL As String
        Dim folderInfo As New System.Text.StringBuilder()

        Dim processedNumberCount As Integer = 0
        Dim numberOfErrors As Integer = 0
        Dim fileSucessfullyDone As Integer
        '  cboPattern.SelectedIndex = 0
        btnList_Click(sender, e)

        For i = 0 To ListBoxFoldersToBeDone.Items.Count - 1
            newFolderURL = ListBoxFoldersToBeDone.Items(i).ToString
            Try
                'must download winZip command line 32 / 64 bit depending on the verion of winZip u have
                Dim dirInfo As New System.IO.DirectoryInfo(newFolderURL)
                Dim folderName As String = (dirInfo.Name)
                reNamedTo = newFolderURL & "\" & folderName & ".zip"


                lblFolderBeingDone.Text = reNamedTo
                psiProcess.FileName = "C:\Program Files\Winzip\wzzip.exe"
                psiProcess.WorkingDirectory = newFolderURL
                psiProcess.WindowStyle = ProcessWindowStyle.Minimized
                psiProcess.ErrorDialog = False
                psiProcess.CreateNoWindow = True
                '  psiProcess.Arguments = ("" & " -a -p -r -m """ & reNamedTo & """ *.tif")
                psiProcess.Arguments = ("" & " -a -p -r """ & reNamedTo & """ *.tif")

                udtProc = Process.Start(psiProcess)
                udtProc.WaitForExit()
                'lblDeletingFiles.Text = reNamedTo
                processedNumberCount = processedNumberCount + 1
                lblCountItemsDone.Text = processedNumberCount.ToString
                pBar1.Value = processedNumberCount
                initStatusBar(processedNumberCount)
                listBoxFoldersDone.Items.Add(reNamedTo)
                deleteAfterZip(newFolderURL)
            Catch ex As Exception
                numberOfErrors = numberOfErrors + 1
                listBoxErrorFolders.Items.Add(reNamedTo)
            End Try
        Next

        udtProc.Close()
        listBoxFiles.Items.Clear()
        Dim title As String = "TIFF Files Zip Completed"
        Dim msg As String = "Process Complete , " & processedNumberCount & " files processed successfully.  " & numberOfErrors.ToString & " error(s) encountered"
        MessageBox.Show(msg, title)
        lblFolderBeingDone.Text = "-"

    End Sub

1 个答案:

答案 0 :(得分:0)

您会对不使用WinZip的其他解决方案持开放态度吗? .Net运行时实际上内置了压缩,最近的版本使我们更容易使用。

要使下面的代码工作,您需要拥有.Net 4.5,并且需要在项目中添加对System.IO.CompressionSystem.IO.Compression.FileSystem的引用。它遍历给定的文件夹,将文件添加到zip文件,并在达到最大字节阈值时创建新的zip文件。此代码的唯一缺点是最大字节阈值是根据未压缩的文件长度计算的,因为压缩的大小直到稍后才知道。但是,根据您的要求,这应该是一个问题。

代码的评论应该有希望解释一切。有两种方法,一种是您不想压缩的可选文件扩展名数组。要使用代码将桌面压缩到“我的文档”文件夹中的文件夹,使用50MB的字节阈值,您只需执行以下操作:

Dim FolderToCompress = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim FolderToSaveZipTo = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ZIPs")

CompressFolder(FolderToCompress, FolderToSaveZipTo, "MyArchive", 1024 * 1024 * 50)

这是代码:

''' <summary>
''' Compress a given folder splitting the zip files based on a supplied maximum uncompressed file length.
''' </summary>
''' <param name="folderToCompress">The folder to compress.</param>
''' <param name="folderToSaveZipFilesTo">The folder to save the compress files to.</param>
''' <param name="zipFileNameWithoutExtension">The file excluding the .zip extension to save to.</param>
''' <param name="maximumBytesToCompressPerFile">The maximum number of uncompress bytes that will be put into each zip file.</param>
''' <param name="fileExtensionsNotToCompress">An array of file extensions, including the leading period, to store only and not compress.</param>
Public Shared Sub CompressFolder(
                                    folderToCompress As String,
                                    folderToSaveZipFilesTo As String,
                                    zipFileNameWithoutExtension As String,
                                    maximumBytesToCompressPerFile As Long,
                                    fileExtensionsNotToCompress() As String
                                )
    ''Create the directory if it doesn't exist already
    Directory.CreateDirectory(folderToSaveZipFilesTo)

    ''Create a formattable string that increments an index each time
    Dim FileNameFormatForZip = zipFileNameWithoutExtension & "_{0}.zip"
    Dim CurrentZipFileIndex = 0

    ''The total amount of uncompressed files process so far
    Dim CurrentUncompressedBytesProcessedSoFar As Long = 0

    ''Objects that we'll init below
    Dim FileStreamForZip As Stream = Nothing
    Dim Zip As ZipArchive = Nothing

    ''Loop through each file in the given directory including all child directories
    For Each FI In Directory.EnumerateFiles(folderToCompress, "*.*", SearchOption.AllDirectories)

        ''Get the local file name relative to the parent directory
        Dim LocalName = FI.Replace(folderToCompress, "").TrimStart("\"c)

        ''If we don't currently have a stream created, create one
        If FileStreamForZip Is Nothing Then
            Dim FileToSaveZipTo = Path.Combine(folderToSaveZipFilesTo, String.Format(FileNameFormatForZip, CurrentZipFileIndex))
            FileStreamForZip = New FileStream(FileToSaveZipTo, FileMode.Create, FileAccess.Write, FileShare.None)

            Zip = New ZipArchive(FileStreamForZip, ZipArchiveMode.Create)
        End If

        ''Set a default compression level
        Dim CompressionLevel = System.IO.Compression.CompressionLevel.Optimal

        ''However, if the current file extension is on our do not compress list then only set it to store
        If fileExtensionsNotToCompress.Contains(New System.IO.FileInfo(FI).Extension) Then
            CompressionLevel = Compression.CompressionLevel.NoCompression
        End If

        ''Create our zip entry
        Dim ZE = Zip.CreateEntry(LocalName, CompressionLevel)

        ''Add our file's contents to the entry
        Using ZipStream = ZE.Open()
            Using FileStream = File.Open(FI, FileMode.Open, FileAccess.Read, FileShare.Read)
                FileStream.CopyTo(ZipStream)
            End Using
        End Using

        ''Increment our file byte counter by the uncompressed file's original size
        ''Unfortunately we can't use the ZE.CompressedLength because that is only available
        ''during the reading of a ZIP file
        CurrentUncompressedBytesProcessedSoFar += New System.IO.FileInfo(FI).Length

        ''If we're over the threshold for maximum bytes
        If CurrentUncompressedBytesProcessedSoFar >= maximumBytesToCompressPerFile Then

            ''Clean up and dispose of our objects
            Zip.Dispose()
            Zip = Nothing
            FileStreamForZip.Dispose()
            FileStreamForZip = Nothing

            ''Reset our counter
            CurrentUncompressedBytesProcessedSoFar = 0

            ''Increment the current file index
            CurrentZipFileIndex += 1
        End If
    Next

    ''Clean up
    If Zip IsNot Nothing Then
        Zip.Dispose()
        Zip = Nothing
    End If

    If FileStreamForZip IsNot Nothing Then
        FileStreamForZip.Dispose()
        FileStreamForZip = Nothing
    End If
End Sub

''' <summary>
''' Compress a given folder splitting the zip files based on a supplied maximum uncompressed file length.
''' </summary>
''' <param name="folderToCompress">The folder to compress.</param>
''' <param name="folderToSaveZipFilesTo">The folder to save the compress files to.</param>
''' <param name="zipFileNameWithoutExtension">The file excluding the .zip extension to save to.</param>
''' <param name="maximumBytesToCompressPerFile">The maximum number of uncompress bytes that will be put into each zip file.</param>
Public Shared Sub CompressFolder(folderToCompress As String, folderToSaveZipFilesTo As String, zipFileNameWithoutExtension As String, maximumBytesToCompressPerFile As Long )
    CompressFolder(folderToCompress, folderToSaveZipFilesTo, zipFileNameWithoutExtension, maximumBytesToCompressPerFile, {".zip", ".mp4", ".wmv"})

End Sub