2013-07-23 5 views
0

나는 C#에서 일부 .pdf 파일을 압축하려고합니다. 내 코드는 잘 작동하지만 pdfs 중 하나의 크기가 클 경우 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(); 
    } 
+0

왜 매개 변수에 'ref'를 사용하고 있습니까? 그것은 당신이 의미하는 바를 이해하지 못하는 것이 분명합니다.이 경우 ** 생략해야합니다. 주의 깊게 읽으십시오 : http://www.yoda.arachsys.com/csharp/parameters.html – spender

+0

ref를 사용하지 않을 때도 동일한 결과가 나옵니다. – Alma

답변

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

      }