2011-04-08 2 views
0

내 응용 프로그램은 성능 및 연결이 끊어진 목적으로 많은 양의 캐시 된 데이터를 로컬 저장소에 저장합니다. SharpZipLib을 사용하여 생성 된 캐시 파일을 압축하려고했지만 약간의 어려움이 있습니다.프로그래밍 방식으로 ZIP 파일 만들기

파일을 만들 수는 있지만 잘못된 파일입니다. Windows의 내장 zip 시스템과 7-zip은 모두 파일이 유효하지 않음을 나타냅니다. SharpZipLib을 통해 프로그래밍 방식으로 파일을 열려고하면 "잘못된 중앙 디렉터리 서명"예외가 발생합니다. 문제의 일부는 MemoryStream에서 zip 파일을 직접 생성하므로 "루트"디렉토리가 없다는 것입니다. SharpZipLib을 사용하여 프로그래밍 방식으로 만드는 방법을 모릅니다.

아래의 EntityManager는 IdeaBlade DevForce에서 생성 한 "datacontext"입니다. 캐싱을 위해 디스크로 직렬화하기 위해 내용을 스트림에 저장할 수 있습니다.

private void SaveCacheFile(string FileName, EntityManager em) 
     { 
      using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(FileName, System.IO.FileMode.CreateNew, isf)) 
       { 
        MemoryStream inStream = new MemoryStream(); 
        MemoryStream outStream = new MemoryStream(); 
        Crc32 crc = new Crc32(); 
        em.CacheStateManager.SaveCacheState(inStream, false, true); 
        inStream.Position = 0; 

        ZipOutputStream zipStream = new ZipOutputStream(outStream); 
        zipStream.IsStreamOwner = false; 
        zipStream.SetLevel(3); 

        ZipEntry newEntry = new ZipEntry(FileName); 
        byte[] buffer = new byte[inStream.Length]; 
        inStream.Read(buffer, 0, buffer.Length); 
        newEntry.DateTime = DateTime.Now; 
        newEntry.Size = inStream.Length; 
        crc.Reset(); 
        crc.Update(buffer); 
        newEntry.Crc = crc.Value; 
        zipStream.PutNextEntry(newEntry); 
        buffer = null; 

        outStream.Position = 0; 
        inStream.Position = 0;     
        StreamUtils.Copy(inStream, zipStream, new byte[4096]); 
        zipStream.CloseEntry(); 
        zipStream.Finish(); 
        zipStream.Close(); 
        outStream.Position = 0; 
        StreamUtils.Copy(outStream, isfs, new byte[4096]); 
        outStream.Close();  

       } 
      } 
     } 

답변

0

메모리에서 직접 zip 파일을 생성한다 없습니다 문제 :

여기 내 코드입니다. SharpZipLib는 ZipEntry 생성자의 매개 변수를 사용하여 경로를 결정하고 해당 경로에 하위 디렉터리가 있는지 여부는 신경 쓰지 않습니다.

using (ZipOutputStream zipStreamOut = new ZipOutputStream(outputstream)) 
{ 
    zipStreamOut.PutNextEntry(new ZipEntry("arbitrary.ext")); 
    zipstreamOut.Write(mybytearraydata, 0, mybytearraydata.Length); 
    zipStreamOut.Finish(); 
    //Line below needed if outputstream is a MemoryStream and you are 
    //passing it to a function expecting a stream. 
    outputstream.Position = 0; 

    //DoStuff. Optional; Not necessary if e.g., outputstream is a FileStream. 
} 
-1

outStream.Position = 0;을 제거하면 작동합니다.