0

WinZip コマンドラインに関するヘルプが必要です。現在、正常に動作するコマンドラインがありますが、フォルダーが 4 GB を超えるとクラッシュします。100MB を超える非常に大きな Tiff ファイルがあります。ファイルが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
4

1 に答える 1

0

WinZip をまったく使用しない別のソリューションを受け入れますか? .Net ランタイムには実際に圧縮機能が組み込まれており、最近のバージョンではさらに使いやすくなっています。

以下のコードが機能するには、.Net 4.5 が必要であり、プロジェクトにSystem.IO.Compressionとの両方への参照を追加する必要がありSystem.IO.Compression.FileSystemます。指定されたフォルダーを移動し、ファイルを zip ファイルに追加し、最大バイト数のしきい値に達すると新しい zip ファイルを作成します。このコードの唯一の欠点は、最大バイト数のしきい値が圧縮されていないファイルの長さに基づいて計算されることです。これは、圧縮されたサイズが後になるまでわからないためです。ただし、要件に基づいて、これは問題になるはずです。

コードのコメントですべてを説明できるはずです。2 つの方法があります。1 つは、圧縮したくないファイル拡張子のオプションの配列を取ります。このコードを使用して、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
于 2015-02-26T16:22:09.743 に答える