2015-08-16 105 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; 
    }