簡體   English   中英

覆蓋壓縮文件

[英]overwrite file in Zipping

我嘗試用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();
    }

我發現了問題。 我必須在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