如何制作一个zip文件
本文关键字:一个 zip 文件 何制作 | 更新日期: 2023-09-27 18:02:58
我有一个上传按钮,当我上传文件时,我的文件必须以zip或压缩形式上传到服务器上的指定路径
我试过了===>
string strFileName = string.Empty;
string strserverPath = string.Empty;
if (UploadFiles.HasFile)
{
string abcPATH = tvFolders.SelectedValue.ToString();
string rootPath = tvFolders.SelectedNode.ToString();
string fname = Path.GetFileName(UploadFiles.PostedFile.FileName);
try
{
strserverPath = abcPATH + "''" + fname;
//string strName = Path.GetFileName(UploadFiles.PostedFile.FileName);
Stream myStream = UploadFiles.PostedFile.InputStream;
byte[] myBuffer = new byte[myStream.Length + 1];
myStream.Read(myBuffer, 0, myBuffer.Length);
myStream.Close();
FileStream myCompressedFile = default(FileStream);
myCompressedFile = File.Create(Server.MapPath(Path.ChangeExtension("~/"+strserverPath, "zip")));
GZipStream myStreamZip = new GZipStream(myCompressedFile, CompressionMode.Compress);
myStreamZip.Write(myBuffer, 0, myBuffer.Length);
myStreamZip.Close();
//asp.net c#
//UploadFiles.PostedFile.SaveAs(Server.MapPath("~/" + strserverPath));
listofuploadedfiles.Text = "done";
}
catch (Exception ex)
{
Response.Write("Error" + ex.Message);
}
}
else
listofuploadedfiles.Text = "not done";
}
在。net 4.5中有一个新的System.IO.Compression.ZipFile命名空间,它有一个友好的api来创建zip文件,而不是使用棘手的。net 3.0包方法。
唯一需要注意的是它对文件夹结构起作用,而不是直接对文件起作用。
正如下面的评论所建议的,这不是一个"让你开始"解决你的确切问题。但也许有一种新的方法从一开始就更容易。
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string startPath = @"c:'example'start";
string zipPath = @"c:'example'result.zip";
string extractPath = @"c:'example'extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
摘自http://msdn.microsoft.com/en-us/library/vstudio/system.io.compression.zipfile
它应该让您开始使用zipfile方法。
。. NET 4.5以下版本本身不支持ZIP文件格式。
您可以查看添加该支持的库,例如SharpZipLib
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
AddFileToZip("Output.zip", @"C:'Windows'Notepad.exe");
AddFileToZip("Output.zip", @"C:'Windows'System32'Calc.exe");
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".''" + 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);
}
}
}
}
private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
{
long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesWritten = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesWritten += bufferSize;
}
}
}
}
编码已通过这些网站参考
1。在。net中创建Zip文件(不需要像SharpZipLib这样的外部库)2. http://madprops.org/blog/zip-your-streams-with-system-io-packaging/
3.以编程方式创建普通zip文件
附加参考:
你可以参考这些网站。它可能对你有用。
http://forums.asp.net/t/1086292.aspx?How + +创建+ zip +文件+使用+ C +
以编程方式创建普通zip文件
http://www.dotnetperls.com/zipfile http://www.aspdotnet-suresh.com/2013/04/create-zip-files-in-aspnet-c-vbnet-zip.html http://www.codeproject.com/Questions/450505/create-zip-file-in-asp-net好运. .