简体   繁体   English

覆盖压缩文件

[英]overwrite file in Zipping

I try to Zip some .pdf files in C#. 我尝试用C#压缩一些.pdf文件。 My code works fine but when the size of one of the pdfs is big, it is going to overwrite that pdf on the rest of pdfs. 我的代码工作正常,但是当其中一个pdf的大小很大时,它将在其余pdf上覆盖该pdf。 I am not sure what is happening. 我不确定发生了什么。 I tried to increase the size of buffer or zip file but still same issue. 我试图增加缓冲区或zip文件的大小,但仍然是同样的问题。 Do you have any suggestion? 你有什么建议吗?

This is my code: 这是我的代码:

    public void ProcessZipRequest(string strQueueID, string strBatchID, string strFtpPath)
    {

        int intReportCnt = 0;

        string strZipFileName = "Order-" + strBatchID + "-" + strQueueID + "-" + DateTime.Now.ToString("MM-dd-yyyy-HH-mm") + ".zip";
        strZipFileName = SafeFileName(strZipFileName);

        //MemoryStream ms = new MemoryStream();
        FileStream ms = new FileStream(@"c:\surf\nikoo.zip", FileMode.Create);
        ZipOutputStream oZipStream = new ZipOutputStream(ms); // create zip stream

        oZipStream.SetLevel(9); // maximum compression

        intReportCnt += 1;

        string strRptFilename=string.Empty;
        MemoryStream outputStream = new MemoryStream();
        if (strQueueID != null)
        {



            String[] filenames = Directory.GetFiles(@"C:\uploaded");

            // setting Report name to path given for Report name

            foreach (String filename in filenames)
            {
                strRptFilename = filename.Substring(filename.LastIndexOf("\\") + 1);



                FileStream fs = File.OpenRead(@"C:\uploaded\" + strRptFilename);
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = fs.Read(buffer, 0, bufferSize);
                while (readCount>0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = fs.Read(buffer, 0, bufferSize);

                }
                fs.Close();
                outputStream.Position = 0;


                ZipFile(ref outputStream, strRptFilename, ref oZipStream);

            }

        }

        outputStream.Close();
        oZipStream.Finish();
        oZipStream.Flush();

        oZipStream.IsStreamOwner = false;  //  False stops the Close also Closing the underlying stream.
        oZipStream.Close();                // Must finish the ZipOutputStream before using outputMemStream.

        ms.Close();

    }

And this is Zipfile Method: 这是Zipfile方法:

   public void ZipFile(ref MemoryStream msFile, string strFilename, ref ZipOutputStream oZipStream)
    {
        ZipEntry oZipEntry = new ZipEntry(strFilename);
        oZipEntry.DateTime = DateTime.Now;
        oZipEntry.Size = msFile.Length;

        oZipStream.PutNextEntry(oZipEntry);

        StreamUtils.Copy(msFile, oZipStream, new byte[4096]);


        oZipStream.CloseEntry();
    }

I found the issue. 我发现了问题。 I have to create a new MemoyStream in for loop and close it at the end of the loop. 我必须在for循环中创建一个新的MemoyStream,并在循环结束时将其关闭。

foreach (String filename in filenames)
            {
                strRptFilename = filename.Substring(filename.LastIndexOf("\\") + 1);

                outputStream = new MemoryStream();

                FileStream fs = File.OpenRead(@"C:\uploaded\" + strRptFilename);

                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = fs.Read(buffer, 0, bufferSize);
                while (readCount>0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = fs.Read(buffer, 0, bufferSize);

                }
                fs.Close();
                outputStream.Position = 0;


                ZipFile(ref outputStream, strRptFilename, ref oZipStream);
                fs.Close();
                outputStream.Close();

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM