C# 如何制作zip文件
我有一个上传按钮,当我上传文件时,我的文件必须以zip或压缩的形式上传到服务器上指定的路径 我试过==>C# 如何制作zip文件,c#,asp.net,gzip,C#,Asp.net,Gzip,我有一个上传按钮,当我上传文件时,我的文件必须以zip或压缩的形式上传到服务器上指定的路径 我试过==> string strFileName = string.Empty; string strserverPath = string.Empty; if (UploadFiles.HasFile) { string abcPATH = tvFolders.SelectedValue.ToString();
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";
}
低于4.5版的.NET本机不支持ZIP文件格式 您可以查看添加该支持的库,例如使用系统的
;
使用System.IO;
使用System.IO.Packaging;
名称空间ZipSample
{
班级计划
{
静态void Main(字符串[]参数)
{
AddFileToZip(“Output.zip”,@“C:\Windows\Notepad.exe”);
AddFileToZip(“Output.zip”,@“C:\Windows\System32\Calc.exe”);
}
private const long BUFFER_SIZE=4096;
私有静态void AddFileToZip(字符串zipFilename,字符串fileToAdd)
{
使用(Package zip=System.IO.Packaging.Package.Open(zipFilename、FileMode.OpenOrCreate))
{
字符串destFilename=“.\\”+Path.GetFileName(fileToAdd);
uriuri=PackUriHelper.CreatePartUri(新Uri(destFilename,UriKind.Relative));
if(zip.PartExists(uri))
{
DeletePart(uri);
}
PackagePart=zip.CreatePart(uri,“,CompressionOption.Normal);
使用(FileStream FileStream=newfilestream(fileToAdd,FileMode.Open,FileAccess.Read))
{
使用(Stream dest=part.GetStream())
{
CopyStream(文件流、目的地);
}
}
}
}
私有静态void CopyStream(System.IO.FileStream inputStream、System.IO.Stream outputStream)
{
long bufferSize=inputStream.Length
编码已经通过这些网站被引用
一,。
2. 三, 其他参考资料: 你可以参考这些网站。它可能对你有用
祝你好运.在.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);
}
}
}
取自
这应该让您开始使用zipfile方法。您看到的问题/错误消息是什么?问题是什么?您正在尝试上载zip文件,但无法执行此操作吗?是的,先生,我在代码中使用了treeview,我想在选定的节点中以zip形式上载我的文件。从.net 4.5开始,就有支持:@Alex tnx,今天学到了一些新东西:)不用担心,烘焙所需的时间令人惊讶。以前人们甚至对ZipPackage类知之甚少,但使用它有点奇怪。当然,使用像SharpZipLib这样的东西会更好。但是新的.NET4.5看起来不错。我看到了你的最后一个链接。在这方面,“使用Ionic.Zip”有什么用,以及我如何使用这个文件bcoz,我以前尝试过使用链接代码来询问qsion,我发现那个.dll文件有问题,所以请告诉我关于Ionic.Zip.dll文件的帮助。OP显示的代码不起作用,而不是要求开始。请使用评论要求澄清,而不是发布一个通用的、重复的答案,或者相应地以重复的形式结束投票。Good point@CodeCaster作为一种新方法,我已经添加了一些细节。先生,我想要动态路径,当我上传exp文件时,我的文件实际上不是zip文件。我现在有xyz.pdf文件。当我上传到服务器时,我的文件必须像xyz.zip一样以zip/rar文件上传/rar@user3622698你可以先为上传的每个文件创建一个文件夹吗?
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);
}
}
}