2015-08-16 126 views
1

我將zip文件轉換爲byte []並將其寫入文本文件中。將Zip文件轉換爲byte []和byte []以壓縮文件

int BufferSize=65536; 
    private void button1_Click(object sender, EventArgs e) 
     { 
      DialogResult re = openFileDialog1.ShowDialog(); 
      if (re == DialogResult.OK) 
      { 
       string fileName = openFileDialog1.FileName; 
       try 
       { 
        byte[] bytes = File.ReadAllBytes(fileName); 
        File.WriteAllBytes(@"F:\Info.txt", bytes); 
       } 
       catch (Exception) { } 
      } 
     } 

然後我嘗試將這些字節轉換爲zip文件。但我做不到。

我的代碼是在這裏:

private void button2_Click(object sender, EventArgs e) 
     { 

      DialogResult re = openFileDialog1.ShowDialog(); 
      if (re == DialogResult.OK) 
      { 
       string fileName = openFileDialog1.FileName; 
       try 
       { 
        byte[] bytes = File.ReadAllBytes(fileName); 
        using (var mstrim = new MemoryStream(bytes)) 
        { 
         using (var inStream = new GZipStream(mstrim, CompressionMode.Compress)) 
         { 
          using (var outStream = File.Create("Tax.Zip")) 
          { 
           var buffer = new byte[BufferSize]; 
           int readBytes; 
           while ((readBytes = inStream.Read(buffer, 0, BufferSize)) != 0) 
           { 
            outStream.Write(buffer, 0, readBytes); 
           } 
          } 
         } 
        } 
       } 
       catch (Exception) { } 
      } 
     } 

Error:File Mode not valid.

+1

這種縮進風格確實沒有幫助。按下Ctrl-K,D。 – usr

+0

其實你必須解壓縮zip文件。從壓縮文件讀取字節不會給你已經壓縮在文件內的實際數據。 這就是爲什麼你不能從你剛剛閱讀的字節重新創建zip文件。 – Rezoan

+0

使用DotNetZIP:https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=示例 與您相比,您可以在比Gzip更少的時間和精力內完成更多的工作。 :) – Rezoan

回答

2

剛剛嘗試這一點。

  byte[] data = File.ReadAllBytes("D:\\z.7z"); 
      File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO 

      byte[] newdata = File.ReadAllBytes("D:\\t.txt"); 
      File.WriteAllBytes("D:\\a.7z", newdata); // Requires System.IO 
0

您使用GZipStream,它用於gzip格式,而不是(PK-)Zip文件。顯然這不起作用。嘗試使用ZipFile類(儘管很遺憾,它不適用於流,只是文件)。

除了僅僅是一個不同的文件格式,最大的區別是GZip僅用於壓縮,而Zip也是一個存檔(即它可以包含多個文件)。

1

試試這個,

private void button1_Click(object sender, EventArgs e) 
    { 
     byte[] arr; 
      MemoryStream ms = new MemoryStream(); 
      arr = File.ReadAllBytes("C:\\asik.zip"); 
      File.WriteAllBytes(@"D:\\asik.txt", arr); 
      ms.Close(); 
      FileStream stream = File.OpenRead(@"D:\\asik.txt"); 
      byte[] fileBytes = new byte[stream.Length]; 
      stream.Read(fileBytes, 0, fileBytes.Length); 
      stream.Close(); 
      MemoryStream ms1 = new MemoryStream(fileBytes); 
      CreateToMemoryStream(ms1, @"D:\\asik.zip"); 
      ms1.Close(); 


    } 

    public void CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) 
    { 

     MemoryStream outputMemStream = new MemoryStream(); 
     ZipOutputStream zipStream = new ZipOutputStream(outputMemStream); 

     zipStream.SetLevel(3); //0-9, 9 being the highest level of compression 

     ZipEntry newEntry = new ZipEntry(zipEntryName); 
     newEntry.DateTime = DateTime.Now; 

     zipStream.PutNextEntry(newEntry); 

     StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]); 
     zipStream.CloseEntry(); 

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

     //outputMemStream.Position = 0; 
     //return outputMemStream; 

     //// Alternative outputs: 
     //// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory. 
     //byte[] byteArrayOut = outputMemStream.ToArray(); 

     //// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself. 
     //byte[] byteArrayOut2 = outputMemStream.GetBuffer(); 
     //long len = outputMemStream.Length; 
    }