gpt4 book ai didi

c# - 使用 SevenZipSharp 压缩多卷?

转载 作者:太空狗 更新时间:2023-10-29 22:23:01 28 4
gpt4 key购买 nike

我想使用 SevenZipsharp 来压缩文件,例如将一个 100 MB 的文件压缩成 10 个文件,每个文件 10 MB。我们可以使用 7zip.exe -volume 参数来做同样的事情......

例如,当我使用 7Zip 压缩多个卷时,我得到这些文件结构:

File.7z.001
File.7z.002
File.7z.003
etc...

文件不是独立的,不是多个相同大小的 7zip 文件,是一个不同卷的压缩文件,我的意思是提取全部内容我们需要第一个文件“File.7z.001”

我想用 SevenZipSharp 做同样的事情(如果可能的话)。

我已经在 vb.net 中制作了这个片段(但无论答案是否在 C# 代码中),我需要实现多卷选项,我需要帮助:

   Imports SevenZip

Dim dll As String = "7z.dll"

Private Function SevenZipSharp_Compress(ByVal Input_DirOrFile As String, _
Optional ByVal OutputFileName As String = Nothing, _
Optional ByVal Format As OutArchiveFormat = OutArchiveFormat.SevenZip, _
Optional ByVal CompressionMode As CompressionMode = CompressionMode.Create, _
Optional ByVal CompressionMethod As CompressionMethod = CompressionMethod.Lzma, _
Optional ByVal CompressionLevel As CompressionLevel = CompressionLevel.Normal, _
Optional ByVal Password As String = Nothing) As Boolean
Try
' Set library path
SevenZipExtractor.SetLibraryPath(dll)


' Create compressor and specify the file or folder to compress
Dim Compressor As SevenZipCompressor = New SevenZipCompressor()

' Set compression parameters
Compressor.CompressionLevel = CompressionLevel ' Archiving compression level.
Compressor.CompressionMethod = CompressionMethod ' Append files to compressed file or overwrite the compressed file.
Compressor.ArchiveFormat = Format ' Compression file format
Compressor.CompressionMode = CompressionMode ' Compression mode
Compressor.DirectoryStructure = True ' Preserve the directory structure.
Compressor.IncludeEmptyDirectories = True ' Include empty directories to archives.
Compressor.ScanOnlyWritable = False ' Compress files only open for writing.
Compressor.EncryptHeaders = False ' Encrypt 7-Zip archive headers
Compressor.TempFolderPath = System.IO.Path.GetTempPath() ' Temporary folder path
Compressor.FastCompression = False ' Compress as fast as possible, without calling events.
Compressor.PreserveDirectoryRoot = True ' Preserve the directory root for CompressDirectory.
Compressor.ZipEncryptionMethod = ZipEncryptionMethod.ZipCrypto ' Encryption method for zip archives.
Compressor.DefaultItemName = "File.7z" ' Item name used when an item to be compressed has no name, for example, when you compress a MemoryStream instance

' Get File extension
Dim CompressedFileExtension As String = Nothing
Select Case Compressor.ArchiveFormat
Case OutArchiveFormat.SevenZip : CompressedFileExtension = ".7z"
Case OutArchiveFormat.BZip2 : CompressedFileExtension = ".bz"
Case OutArchiveFormat.GZip : CompressedFileExtension = ".gzip"
Case OutArchiveFormat.Tar : CompressedFileExtension = ".tar"
Case OutArchiveFormat.XZ : CompressedFileExtension = ".xz"
Case OutArchiveFormat.Zip : CompressedFileExtension = ".zip"
End Select

' Add Progress Handler
'AddHandler Compressor.Compressing, AddressOf SevenZipSharp_Compress_Progress

' Removes the end slash ("\") if given for a directory
If Input_DirOrFile.EndsWith("\") Then Input_DirOrFile = Input_DirOrFile.Substring(0, Input_DirOrFile.Length - 1)

' Generate the OutputFileName if any is given.
If OutputFileName Is Nothing Then _
OutputFileName = (My.Computer.FileSystem.GetFileInfo(Input_DirOrFile).DirectoryName & "\" & (Input_DirOrFile.Split("\").Last) & CompressedFileExtension).Replace("\\", "\")

' Check if given argument is Dir or File ...then start the compression
If IO.Directory.Exists(Input_DirOrFile) Then ' Is a Dir
If Not Password Is Nothing Then
Compressor.CompressDirectory(Input_DirOrFile, OutputFileName, True, Password)
Else
Compressor.CompressDirectory(Input_DirOrFile, OutputFileName, True)
End If
ElseIf IO.File.Exists(Input_DirOrFile) Then ' Is a File
If Not Password Is Nothing Then
Compressor.CompressFilesEncrypted(OutputFileName, Password, Input_DirOrFile)
Else
Compressor.CompressFiles(OutputFileName, Input_DirOrFile)
End If
End If

Catch ex As Exception
'Return False ' File not compressed
Throw New Exception(ex.Message)
End Try

Return True ' File compressed


End Function

片段的用法:

   SevenZipSharp_Compress("C:\File or folder", _
"Optional: Output dir", _
OutArchiveFormat.SevenZip, _
CompressionMode.Create, _
CompressionMethod.Lzma, _
CompressionLevel.Ultra, _
"Optional: Password")

最佳答案

在您的 Compressor 对象上,将 VolumeSize 设置为所需的输出文件大小(以字节为单位)。使用下面的示例代码,压缩器创建了 8 个名为 zipped.7z.001、zipped.7z.002 等的输出文件,源目录中包含大约 72MB 的 mp3 文件。

查看 SevenZipExtractor 类的当前源代码,看起来只有当您使用 OutArchiveFormat.SevenZip 作为您的 时,多卷压缩才可用存档格式

string dll = @"C:\Users\WarrenG\Desktop\7z.dll";
string source = @"C:\Users\WarrenG\Desktop\source";
string output = @"C:\Users\WarrenG\Desktop\output\zipped.7z";

SevenZipExtractor.SetLibraryPath(dll);
SevenZipCompressor compressor = new SevenZipCompressor();
compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
compressor.CompressionMode = CompressionMode.Create;
compressor.TempFolderPath = System.IO.Path.GetTempPath();
compressor.VolumeSize = 10000000; // output file size in bytes
compressor.CompressDirectory(source, output);

关于c# - 使用 SevenZipSharp 压缩多卷?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16016540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com