Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends how to convert zip files in to byte arrays in c#
Posted
Comments
Pete O'Hanlon 30-Apr-12 8:38am    
You were told how to do this at: http://www.codeproject.com/Answers/375941/How-to-pass-one-Zip-file-path-to-another-folder-th#answer1. Why did you ignore that answer?

Try this:
C#
private byte [] StreamFile(string filename)
{
   FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

   // Create a byte array of file stream length
   byte[] ImageData = new byte[fs.Length];

   //Read block of bytes from stream into the byte array
   fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));

   //Close the File Stream
   fs.Close();
   return ImageData; //return the byte data
}



and have a look on CP article:
Converting and Compressing a Dataset to Byte array[^]

Check this for reference:
Zip Compression C# Examples[^]
 
Share this answer
 
Comments
Nilesh Patil Kolhapur 30-Apr-12 8:21am    
sry same answers twice
Prasad_Kulkarni 30-Apr-12 8:33am    
Let it be Nilesh, it happens!
Pete O'Hanlon 30-Apr-12 8:40am    
Why not use the File.ReadAllBytes method advocated by Mantu Singh? You've just reinvented something that MS has already provided for you.
Short and simple use the following:
C#
using System.IO;

byte [] data=File.ReadAllBytes("C:\\file.zip");
Best of luck.

[Edit]Added pre tags.
 
Share this answer
 
v2
Comments
Mantu Singh 30-Apr-12 9:07am    
Thanks a lot sir.....!
VJ Reddy 1-May-12 0:12am    
Good answer. 5!
Mantu Singh 1-May-12 5:14am    
Thanks a lot sir.....
Hi,

try Following Code
C#
private byte[] StreamFile(string filename)
    {
        FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

        // Create a byte array of file stream length
        byte[] byteData = new byte[fs.Length];

        //Read block of bytes from stream into the byte array
        fs.Read(byteData, 0, System.Convert.ToInt32(fs.Length));

        //Close the File Stream
        fs.Close();
        return byteData; //return the byte data
    }


Hope this Code help U
Best Luck
Happy Coding:)
 
Share this answer
 
Comments
Pete O'Hanlon 30-Apr-12 8:40am    
Why not use the File.ReadAllBytes method advocated by Mantu Singh? You've just reinvented something that MS has already provided for you.
Member 10158603 2-Aug-13 19:57pm    
Hi I have a zip file that contains several pdfs. I used your method to convert them to byte array and send it to the method. When I open my zipfile pdfs are there but when I try to open pdfs they are corrupted. Any solution for that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900