我在我的项目中使用DotNetZip.
using (var zip = new ZipFile()) 
{ 
   zip.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding(866); 
   zip.AddFile(filename, "directory\\in\\archive"); 
   zip.Save("archive.zip"); 
} 
一切都好,但是当我使用方法AddDirectoryByName我有一个错误的目录名称.
我正在尝试使用DotNetZip组件在MVC方法中创建一个zipfile.
这是我的代码:
    public FileResult DownloadImagefilesAsZip()
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var zip = new ZipFile())
            {
                zip.AddDirectory(Server.MapPath("/Images/"));
                zip.Save(memoryStream);
                return File(memoryStream, "gzip", "images.zip");
            }
        }
    }
当我运行它时,我得到一个"无法访问关闭的流"错误,我不知道为什么.
我尝试将DotNetZip与C++/CLR一起使用但是我发现我下载的每个文件都没有.h文件,在示例代码中,有"using namespace Ionic :: Zip;" 
如何在我的代码中使用它?
尽管找到了例子,但我在这方面遇到了一些麻烦。我认为这可能是一个编码问题,但我不确定。我正在尝试以编程方式从使用 cookie 的 https 服务器下载文件(因此我使用 httpwebrequest)。我正在调试打印要检查的流的容量,但输出[原始]文件看起来不同。尝试过其他编码方式均无效。
代码:
    Sub downloadzip(strURL As String, strDestDir As String)
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse
    request = Net.HttpWebRequest.Create(strURL)
    request.UserAgent = strUserAgent
    request.Method = "GET"
    request.CookieContainer = cookieJar
    response = request.GetResponse()
    If response.ContentType = "application/zip" Then
        Debug.WriteLine("Is Zip")
    Else
        Debug.WriteLine("Is NOT Zip: is " + response.ContentType.ToString)
        Exit Sub
    End If
    Dim intLen As Int64 = response.ContentLength
    Debug.WriteLine("response length: " + intLen.ToString)
    Using srStreamRemote As StreamReader = New StreamReader(response.GetResponseStream(), Encoding.Default)
        'Using ms As New MemoryStream(intLen) …DotNetZip有一个奇怪的问题,我似乎无法找到解决方案.我现在已经搜索了几个小时而且我找不到任何关于此的内容,所以这里有.
var ms = new MemoryStream();
using (var archive = new Ionic.Zip.ZipFile()) {
    foreach (var file in files) {
        //                                string     byte[]
        var entry = archive.AddEntry(file.Name, file.Data);
        entry.ModifiedTime = DateTime.Now.AddYears(10); // Just for testing
    }
    archive.Save(ms);
}
return ms.GetBuffer();
我需要添加修改时间,这是非常关键的,但是现在我只有一个虚拟时间戳.
当我用WinRAR打开文件时,它会显示"意外的归档结束".每个单独的文件都有校验和00000000,WinRAR说"存档格式未知或已损坏".我可以修理它,它可以减少20%的尺寸并使一切正常.但这并不是真的有用..
当我在添加所有条目后创建断点时,我可以看到zip.Entries所有条目都具有相同的错误CRC,但所有数据似乎都存在.所以它不应该是我保存存档的问题.
我在其他地方使用我的文件集没有问题,这增加了DotNetZip很奇怪.好吧,或者我误解了一些东西:)
我正在使用DotNetZip将文件添加到zip存档中,我已从文件系统中读取该文件.我想将生成的ZipFile转换为byte []数组.任何帮助将受到高度赞赏.我的代码如下所示.
public byte[] AddPrjFile(FileStream shapeFileZip, Uri prjLocation)
    {
        string prjFileAbsPath = prjLocation.AbsolutePath;
        using (ZipFile zip = ZipFile.Read(shapFileZip))
        {
            ZipEntry e = zip.AddFile(prjFileAbsPath);
            e.FileName = zipFile.Name + ".prj";
        }
   return byte_array;
}
我有以下代码:
public void extractZipFile()
{
    if (!System.IO.Directory.Exists(extractDirectory))
        System.IO.Directory.CreateDirectory(extractDirectory);
    BackgroundWorker worker = new BackgroundWorker();
    worker.WorkerReportsProgress = true;
    worker.ProgressChanged += (o, e) =>
    {
        progbarExtract.Value = Convert.ToInt32(e.ProgressPercentage);
    };
    lblExtracting.Text = "Extracting...";
    worker.DoWork += (o, e) =>
    {
        using (ZipFile zip = ZipFile.Read(zipFile))
        {
            int step = Convert.ToInt32(zip.Count / 100.0); 
            int percentComplete = 0; 
            foreach (ZipEntry file in zip)
            {
                file.Extract(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XBMC Library Importer\\XBMC_Files", ExtractExistingFileAction.OverwriteSilently);
                    percentComplete += step; //When I comment this out I don't get an exception
                    worker.ReportProgress(percentComplete);
            }
        }
    }; …I am trying to check if a folder exists in a zip file. The code is following:
//All entries refered too exists.
//For files (Workes fine, returns true)
var hello1 = zip.Any(entry => entry.FileName.Equals(@"Patients.xml"));
var hello2 = zip.Any(entry => entry.FileName.Equals(@"Bookings.xml"));
//For folders (Dosent work (returns false))
var result1 = zip.Any(entry => entry.FileName.Equals(@"PatientsF"));
var result2 = zip.Any(entry => entry.FileName.Equals(@"U14"));
I have tryed with:
entry.FileName.Contains(@"PatientsF"));
这是有效的,但我想获得具有确切名称"PatientsF"的文件夹.使用代码"Contains",如果名称只有"PatientsF",则返回true.我该如何解决这个问题?
任何帮助将不胜感激.提前致谢.
PS.如果我不清楚某处,或者您需要更多信息,那么只需解释一下需要什么.
我正在开发一个项目,我需要能够解压缩流和字节数组以及压缩它们.我正在运行一些单元测试,从流中创建Zip然后解压缩它们,当我解压缩它们时,DonNetZip将它们视为拉链的唯一方法就是我运行streamToZip.Seek(o,SeekOrigin.Begin)和streamToZip.Flush().如果我不这样做,我会收到错误" 无法读取阻止,无数据 " ZipFile.Read(stream).
我想知道是否有人能解释为什么会这样.我已经看过一些关于使用它来实际设置相对读取位置的文章,但没有一篇真正解释为什么在这种情况下需要它.
这是我的代码:
压缩对象:
   public Stream ZipObject(Stream data)
    {
        var output = new MemoryStream();
        using (var zip = new ZipFile())
        {
            zip.AddEntry(Name, data);
            zip.Save(output);
            FlushStream(output);
            ZippedItem = output;
        }
        return output;
    }
解压缩对象:
 public List<Stream> UnZipObject(Stream data)
    {
        ***FlushStream(data); // This is what I had to add in to make it work***
        using (var zip = ZipFile.Read(data))
        {
            foreach (var item in zip)
            {
                var newStream = new MemoryStream();
                item.Extract(newStream);
                UnZippedItems.Add(newStream);
            } …我已经构建了一个包含多个条目的DotNetZip ZipFile.我想将它转换为字节数组,以便我可以使用下面的下载构造下载它.
   Using wrkZip As New ZipFile
        '----- create zip, add memory stream----------
       For n As Integer = 0 To wrkAr.Count - 1
           wrkFS = wrkAr(n)
           wrkZip.AddEntry(wrkFS.FileName, wrkFS.ContentStream)
       Next
   dim wrkBytes() as Byte
   dim wrkFileName as string = "Test.txt"
   ===> wrkBytes = ConvertToByteArray(wrkZip) <==== 
    context.Response.Clear()
        context.Response.ContentType = "application/force-download"
        context.Response.AddHeader("content-disposition", "attachment; filename=" & wrkFileName)
        context.Response.BinaryWrite(wrkBytes)
        wrkBytesInStream = Nothing
        context.Response.End()
我认识到有一个ZipFile方法:
wrkZip.Save(context.Response.OutputStream)
但是,我在使用它时遇到了一个难题,如下所述:
所以我正在寻找一个短期的解决方法.关于该漏洞的简短故事是ZipFile写入磁盘,并在一个非常相似的网站上下载; 它只是在我现在需要它的情况下不起作用.
那么,如何将DotNetZip ZipFile转换为字节数组呢?我查看了其他答案,但是他们没有描述转换整个加载的ZipFile的特殊情况.