将zip条目提取到另一个Zip文件 [英] Extract zip entries to another Zip file

查看:115
本文介绍了将zip条目提取到另一个Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我以下代码为什么不起作用?我正在将SharpZipLib API用于Zip流,这是他们网站今天发布的最新版本DL.我试图使用此逻辑将一个zip文件的内容合并到另一个文件中,而不必在磁盘上执行IO,因为预期的zip文件可能包含Windows的保留文件名.我已尝试使用多个不同的源和目标zip文件(包含保留名称的文件和不包含保留名称的文件)进行此操作.该代码不会引发任何异常,并且如果您在每次写操作之前检查缓冲区,您会看到它包含真实数据,但是在整个操作完成之后,目标zip文件的大小没有改变,您可以浏览确认没有新文件(应该添加代码的文件)实际已添加到目标文件. :(

Can anyone tell me why the following code doesnt work? I am using the SharpZipLib API for the Zip streams, latest version DL'ed today from their site. Im attempting to use this logic to merge the contents of one zip file into another, without having to perform IO on the disk, as the intended zip files may contain reserved file names for windows. I have tried this with multiple different source and destination zip files (those that contain reserved names and those that dont). The code does not throw any exception, and if you inspect the buffer prior to each write operation, you can see that it contains real data, yet after the entire operation finishes the size of the target zip file has not changed, and you can explore it to confirm that no new files (the ones the code is supposed to add) have actually been added to the destination file. :(

    public static void CopyToZip(string inArchive, string outArchive)
    {

        ZipOutputStream outStream = null;
        ZipInputStream inStream = null;
        try
        {
            outStream = new ZipOutputStream(File.OpenWrite(outArchive));
            outStream.IsStreamOwner = false;
            inStream = new ZipInputStream(File.OpenRead(inArchive));
            ZipEntry currentEntry = inStream.GetNextEntry();
            while (currentEntry != null)
            {

                byte[] buffer = new byte[1024];
                ZipEntry newEntry = new ZipEntry(currentEntry.Name);
                newEntry.Size = currentEntry.Size;
                newEntry.DateTime = currentEntry.DateTime;
                outStream.PutNextEntry(newEntry);
                int size = 0;
                while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, size);
                }
                outStream.CloseEntry();

                currentEntry = inStream.GetNextEntry();
            }
            outStream.IsStreamOwner = true;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            try { outStream.Close(); }
            catch (Exception ignore) { }
            try { inStream.Close(); }
            catch (Exception ignore) { }
        }      
    }

推荐答案

我最终使用其他API进行了此操作.来自 http://dotnetzip.codeplex.com/的DotNet zip.这是实现:

I ended up doing this using a different API. DotNet zip from http://dotnetzip.codeplex.com/. Here is the implementation:

    public static void CopyToZip(string inArchive, string outArchive, string tempPath)
    {
        ZipFile inZip = null;
        ZipFile outZip = null;

        try
        {
            inZip = new ZipFile(inArchive);
            outZip = new ZipFile(outArchive);
            List<string> tempNames = new List<string>();
            List<string> originalNames = new List<string>();
            int I = 0;
            foreach (ZipEntry entry in inZip)
            {
                if (!entry.IsDirectory)
                {
                    string tempName = Path.Combine(tempPath, "tmp.tmp");
                    string oldName = entry.FileName;
                    byte[] buffer = new byte[4026];
                    Stream inStream = null;
                    FileStream stream = null;
                    try
                    {
                        inStream = entry.OpenReader();
                        stream = new FileStream(tempName, FileMode.Create, FileAccess.ReadWrite);
                        int size = 0;
                        while ((size = inStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stream.Write(buffer, 0, size);
                        }
                        inStream.Close();
                        stream.Flush();
                        stream.Close();
                        inStream = new FileStream(tempName, FileMode.Open, FileAccess.Read);

                        outZip.AddEntry(oldName, inStream);
                        outZip.Save();
                    }
                    catch (Exception exe)
                    {
                        throw exe;
                    }
                    finally
                    {
                        try { inStream.Close(); }
                        catch (Exception ignore) { }
                        try { stream.Close(); }
                        catch (Exception ignore) { }
                    }
                }
            }

        }
        catch (Exception e)
        {
            throw e;
        }
    }

这篇关于将zip条目提取到另一个Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆