0

我尝试在 C# 中压缩一些 .pdf 文件。我的代码工作正常,但是当其中一个 pdf 的大小很大时,它将覆盖其余 pdf 上的那个 pdf。我不确定发生了什么。我试图增加缓冲区或 zip 文件的大小,但仍然是同样的问题。你有什么建议吗?

这是我的代码:

    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();

    }

这是 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();
    }
4

1 回答 1

-1

我发现了这个问题。我必须在 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();

            }
于 2013-07-30T00:19:11.527 回答