我不能在名称空间"System.IO.Compression"中使用"Zipfile"类我的代码是:
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, CompressionLevel.Fastest,true);
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
"zipfile"这个名称在当前上下文中不存在
我怎么解决呢?
我试图从一个条目的代码创建一个新的ZIP包,并将ZIP包保存到文件.我试图用System.IO.Compression.ZipArchive类来实现这一点.我正在使用以下代码创建ZIP包:
using (MemoryStream zipStream = new MemoryStream())
{
using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
var entry = zip.CreateEntry("test.txt");
using (StreamWriter sw = new StreamWriter(entry.Open()))
{
sw.WriteLine(
"Etiam eros nunc, hendrerit nec malesuada vitae, pretium at ligula.");
}
Run Code Online (Sandbox Code Playgroud)
然后我将ZIP保存到WinRT中的文件:
var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("test.zip", CreationCollisionOption.ReplaceExisting);
zipStream.Position = 0;
using (Stream s = await file.OpenStreamForWriteAsync())
{
zipStream.CopyTo(s);
}
Run Code Online (Sandbox Code Playgroud)
或者在普通的.NET 4.5中:
using (FileStream fs = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
{
zipStream.Position = 0;
zipStream.CopyTo(fs);
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法在Windows资源管理器,WinRAR等中打开生成的文件.(我检查生成的文件的大小是否与zipStream的长度匹配,因此流本身已正确保存到文件中.)
我是做错了什么或者ZipArchive类有问题吗?
我使用的是.NET 4.5,如果我尝试使用"CreateFromDirectory"压缩整个目录,ZipFile类的效果很好.但是,我只想在目录中压缩一个文件.我试着指向一个特定的文件(文件夹\ data.txt),但这不起作用.我考虑过ZipArchive类,因为它有一个"CreateEntryFromFile"方法,但似乎这只允许你创建一个现有文件的条目.
有没有办法简单地压缩一个文件而不创建一个空的zipfile(有问题),然后使用ZipArchiveExtension的"CreateEntryFromFile"方法?
**这也是假设我正在开发一个目前无法使用第三方附加组件的公司计划.
示例来自:http://msdn.microsoft.com/en-us/library/ms404280%28v=vs.110%29.aspx
string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";
ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);
Run Code Online (Sandbox Code Playgroud)
但是如果是startPath @"c:\example\start\myFile.txt;",则会抛出目录无效的错误.
我尝试使用c#在内存中创建一个zip文件,但结果是一个zip文件,里面有损坏的文件.所有zip文件都在数据库中.我存储字节.文件是PDF.我的代码如下
//[extract bytes and file name for every file]
using (var zipArchiveMemoryStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(zipArchiveMemoryStream,
ZipArchiveMode.Create, true))
{
foreach (var file in fileData)
{
var zipEntry = zipArchive.CreateEntry(file.FileName);
using (var entryStream = zipEntry.Open())
{
using (var tmpMemory = new MemoryStream(file.Bytes))
{
tmpMemory.CopyTo(entryStream);
};
}
}
}
zipArchiveMemoryStream.Position = 0;
result = zipArchiveMemoryStream.GetBuffer();
//[download zip file]
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的支持
TONIGNO解决方案: 问题是我使用GetBuffer(); 而是ToArray();
using (var zipArchiveMemoryStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(zipArchiveMemoryStream, ZipArchiveMode.Create, …Run Code Online (Sandbox Code Playgroud)How would I basically use ZipFile.CreateFromDirectory to send back zipfile to memory stream instead of a output path.
Or would I have to use ZipArchive and generate the zip file myself? Seems kind of odd there isn't a method for stream.
Here's basically what I'm trying to do
using (MemoryStream ms = new MemoryStream())
{
ZipFile.CreateFromDirectory(path, ms)
buf = ms.ToArray();
LogZipFile(path, filesize, buf.LongLength);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 C# MVC 项目中编写一个方法,该方法从 S3(或任何地方)流式传输文件,并将其动态压缩为 zip 文件,然后再将压缩流发送给用户。到目前为止,我已经找到了几种通过将流保存到磁盘然后正常返回来从流创建 zip 文件的方法,但我想跳过保存到磁盘并使用缓冲区到流的方法。我正在尝试下载一个非常大的文件(4GB+),该文件很容易压缩到其原始大小的一小部分。
到目前为止,我已经避免了磁盘,但似乎首先将整个文件加载到内存中:
using( var memoryStream = new MemoryStream() )
{
using( var archive = new ZipArchive( memoryStream, ZipArchiveMode.Create, true ) )
{
var zipEntry = archive.CreateEntry( File );
using( var entryStream = zipEntry.Open() )
{
S3.StreamFile( File, Bucket ).CopyTo( entryStream );
}
}
return base.File( memoryStream.ToArray(), "application/zip", File + ".zip" );
}
Run Code Online (Sandbox Code Playgroud)
类似的问题(Creating a ZIP Archive in Memory using System.IO. Compression)仅包含涉及写入磁盘的答案。
我正在开发的应用程序需要将xml文件压缩为zip文件,并通过http请求将其发送到Web服务。因为我不需要保留zip文件,所以我只是在内存中执行压缩。Web服务拒绝了我的请求,因为zip文件显然格式错误。
我知道此问题中有一个解决方案可以很好地工作,但它使用了StreamWriter。该解决方案的我的问题是,StreamWriter需要编码或假设UTF-8,并且我不需要知道xml文件的编码。我只需要从这些文件中读取字节,然后将它们存储在zip文件中,无论它们使用哪种编码即可。
因此,很明显,这个问题与编码无关,因为我不需要将字节转换为文本或相反。我只需要压缩一个byte[]。
我正在使用下一个代码来测试zip文件的格式是否错误:
static void Main(string[] args)
{
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
string xmlDeclaration = "<?xml version=\"1.0\" encoding=\"" + encoding.WebName.ToUpperInvariant() + "\"?>";
string xmlBody = "<Test>ª!\"·$%/()=?¿\\|@#~€¬'¡º</Test>";
string xmlContent = xmlDeclaration + xmlBody;
byte[] bytes = encoding.GetBytes(xmlContent);
string fileName = "test.xml";
string zipPath = @"C:\Users\dgarcia\test.zip";
Test(bytes, fileName, zipPath);
}
static void Test(byte[] bytes, string fileName, string zipPath)
{
byte[] zipBytes;
using (var memoryStream = new MemoryStream())
using (var zipArchive = …Run Code Online (Sandbox Code Playgroud) c# ×7
zip ×4
.net ×2
compression ×2
amazon-s3 ×1
c#-4.0 ×1
windows-8 ×1
ziparchive ×1
zipfile ×1