我想知道我们是否可以使用.net class ZipPackage将文件夹压缩到file.zip
文件。然后,我想打开file.zip
并添加更多文件/文件夹。有可能吗?
[编辑]
我正在尝试使用原生的.net库
答案 0 :(得分:4)
本机库不是纯拉链,但它可用于存档文件。它会在zip的根目录中添加额外的文件。如果你不介意这个额外的文件,那么使用它。还有其他库,以正确的方式存档,更快,更易于使用,并具有矿石功能:DotNetZip和SharpZipLib。
答案 1 :(得分:2)
Package
类用于处理包,它使用zip格式进行存储,但zip中包含特殊元文件。包是zip,但所有zip文件都不是包。您只能使用Package
类打开包,而不能使用任何zip文件打开包。
第三方库可能会更好,因为您不限于包的形式。
答案 2 :(得分:1)
您可以使用.NET中仍然支持的J#库提供的本机Zip类 - 您必须在项目中添加对vjslib
的引用以启用它们。
示例代码:
using java.io;
using java.util.zip;
...
private void ZipFile(string sourceFile, string targetName, ref ZipOutputStream zipout)
{
// read the temporary file to a file source
java.io.FileInputStream fileSourceInputStream = new java.io.FileInputStream(sourceFile);
// create a zip entry
ZipEntry ze = new ZipEntry(targetName);
ze.setMethod(ZipEntry.DEFLATED);
zipout.putNextEntry(ze);
// stream the file to the zip
CopyStream(fileSourceInputStream, zipout);
zipout.closeEntry();
// flush all data to the zip
fileSourceInputStream.close();
zipout.flush();
}
private static void CopyStream(java.io.InputStream from, java.io.OutputStream to)
{
BufferedInputStream input = new BufferedInputStream(from);
BufferedOutputStream output = new BufferedOutputStream(to);
sbyte[] buffer = new sbyte[16384];
int got;
while ((got = input.read(buffer, 0, buffer.Length)) > 0)
{
output.write(buffer, 0, got);
}
output.flush();
}
答案 3 :(得分:1)
谢谢大家!你的任何建议都对我很有帮助。
由于我更喜欢使用原生支持的.net库,我将自己投入ZipPackage类,经过一段时间的尝试,我对自己问题的回答是yes
。
最后,我已经想出如何使用它来将文件/文件夹添加到包中。这是my modified version,它来自sample on MSDN,允许我们这样做。
答案 4 :(得分:1)
这是我开发的内容,2个签名,因此您可以在zip文件中创建目录:
Imports System.IO
Imports System.IO.Packaging
Public Class clsCompression
Const BUFFER_SIZE As Long = 4096
Sub New()
End Sub
Public Function AddFileToZip(ByVal zipFilename As String, _
ByVal fileToAdd As String, _
ByVal directoryFile As String) As Boolean
Dim trace As String = ""
Try
trace = String.Format("{0} AddFileToZip zipFilename: '{1}' fileToAdd: '{2}'{3}", _
Now, zipFilename, fileToAdd, vbNewLine)
Using zip As Package = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)
Dim destFilename As String = ".\" & directoryFile & "\" & Path.GetFileName(fileToAdd)
Dim uri As Uri = PackUriHelper.CreatePartUri(New Uri(destFilename, UriKind.Relative))
If zip.PartExists(uri) Then
zip.DeletePart(uri)
End If
Dim part As PackagePart = zip.CreatePart(uri, "", CompressionOption.Normal)
Using fileStream As New FileStream(fileToAdd, FileMode.Open, FileAccess.Read)
Using dest As Stream = part.GetStream()
CopyStream(fileStream, dest)
End Using
End Using
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function AddFileToZip(ByVal zipFilename As String, ByVal fileToAdd As String) As Boolean
Dim trace As String = ""
Try
trace = String.Format("{0} AddFileToZip zipFilename: '{1}' fileToAdd: '{2}'{3}", _
Now, zipFilename, fileToAdd, vbNewLine)
Using zip As Package = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)
Dim destFilename As String = ".\" & Path.GetFileName(fileToAdd)
Dim uri As Uri = PackUriHelper.CreatePartUri(New Uri(destFilename, UriKind.Relative))
If zip.PartExists(uri) Then
zip.DeletePart(uri)
End If
Dim part As PackagePart = zip.CreatePart(uri, "", CompressionOption.Normal)
Using fileStream As New FileStream(fileToAdd, FileMode.Open, FileAccess.Read)
Using dest As Stream = part.GetStream()
CopyStream(fileStream, dest)
End Using
End Using
End Using
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub CopyStream(ByVal inputStream As System.IO.FileStream, ByVal outputStream As System.IO.Stream)
Dim bufferSize As Long = If(inputStream.Length < BUFFER_SIZE, inputStream.Length, BUFFER_SIZE)
Dim buffer As Byte() = New Byte(CInt(bufferSize) - 1) {}
Dim bytesRead As Integer = 0
Dim bytesWritten As Long = 0
While (InlineAssignHelper(bytesRead, inputStream.Read(buffer, 0, buffer.Length))) <> 0
outputStream.Write(buffer, 0, bytesRead)
bytesWritten += bufferSize
End While
End Sub
Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
End Class
答案 5 :(得分:0)
可以使用纯.NET 3.0完成。由于修改了GPL许可证,可能不希望使用SharpZipLib。
首先,您需要对WindowsBase.dll的引用。
此代码将打开或创建一个zip文件,在其中创建一个目录,然后将该文件放在该目录中。如果要压缩可能包含子目录的文件夹,则可以遍历目录中的文件并为每个文件调用此方法。然后,您可以先深度搜索子目录中的文件,为每个子目录调用方法,然后传递路径以在zip文件中创建该层次结构。
public void AddFileToZip(string zipFilename, string fileToAdd, string destDir)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = "." + destDir + "\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}
destDir可以是一个空字符串,它将直接将文件放在zip中。
https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging