Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on this application that enables user to log in into another website, and then download specified file from that server. So far I have succeeded in logging on the website and download the file. But everything ruins when it comes to zip files.

Is there any chunk of code that could be helpful in reading the .zip files byte by byte or by using stream reader?

I m using
C#
WebClient.DownloadFile()
but its not returning the correct zip file.

I need a method by which I can read zip files. Can I do it by using
C#
ByteReader()

Thanks in advance.
Posted
Updated 10-Nov-22 19:13pm
Comments
John Orendt 26-Jun-12 6:09am    
Do you want to unzip the files in the zip file?
akhilgaur1988 26-Jun-12 7:01am    
No actually i just want to download the zip file as it is on the server.
But each time I try to download the file, server returns me an invalid zip file. And when I try to view it on the notepad it contains following lines "Virtual user BD1234 logged in."
.

C#
WebClient client = new WebClient();

client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);

client.DownloadDataAsync(new Uri("http://www.test.com/myzip.zip"), @"C\newfile.zip");


C#
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
   // MessageBox.Show("File downloaded");
}
 
Share this answer
 
Comments
akhilgaur1988 26-Jun-12 7:10am    
It returns an invalid zip file. Can't just read the zip file or copy the contents of zip file into another zip file? Is there any way to do this?
step 1:

To download zip file from another server to local server
C#
string remoteFilePath = "http://yourdomain.com/test/aa.zip";
        Uri remoteFilePathUri = new Uri(remoteFilePath);
        string remoteFilePathWithoutQuery = remoteFilePathUri.GetLeftPart(UriPartial.Path);
        string fileName = Path.GetFileName(remoteFilePathWithoutQuery);
        string localPath = Server.MapPath("~") + @"test\" + fileName;
        WebClient webClient = new WebClient();
        webClient.DownloadFile(remoteFilePath, localPath);
        webClient.Dispose();


step 2:

To download file from local server by web page
C#
Response.Clear();
       Response.Charset = "";
       Response.ContentType = "application/zip";
       Response.AppendHeader("Content-Disposition", "attachment; filename=aa.zip");
       Response.TransmitFile(localPath);
       Response.Flush();
       Response.End();
 
Share this answer
 
v2
Comments
akhilgaur1988 27-Jun-12 0:46am    
your help is really appreciable but I have already discussed above. This

<pre lang="c#"> WebClient webClient = new WebClient();

webClient.DownloadFile(remoteFilePath, localPath);

webClient.Dispose(); </pre>

This returns me a zip file while which is corrupted or invalid. And when I open that zip file in notepad it contains a line ""Virtual user BD1234 logged in."
akhilgaur1988 wrote:
Is there any chunk of code that could be helpful in reading the .zip files byte by byte or by using stream reader?

I just used ICSharpCode.SharpZipLib last week to peek inside flash swf files, because most of the time, the files are compressed.

I can't remember where I got it from, but it was a free download. It seems to handle almost everything, zip, rar, tar, and the source is available as well.

I used it like this below
Dim inputStream As Stream = Nothing
If (System.Text.Encoding.ASCII.GetString(signature, 0, 3) = "CWS") Then
   inputStream = New InflaterInputStream(stream)
Else
   inputStream = stream
End If
 
Share this answer
 
v2
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
byte[] buffer = new byte[32768];
using (Stream input = objResponse.GetResponseStream())
{
using (FileStream output = new FileStream ("test.doc",
FileMode.CreateNew))
{
int bytesRead;

while ( (bytesRead=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}


This is how i achieved it. Thanks everyone for ur help
 
Share this answer
 
The below code exports a ZIP file from Yammer API (Data Export API)
Documentation: Network Data Export - Yammer | Microsoft Learn[^]


C#
public void Main()
		{
            try
            {
                int te = 1;
                string AccessToken = "xxx";
                string BaseUrl = "https://export.yammer.com/api/v1/export";

                string url = BaseUrl + "?since=2022-01-01T00:00:00+00:00&model=Group";

                WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
                objRequest.Headers.Add("Authorization", "Bearer " + AccessToken);
                objRequest.ContentType = "application/zip";
                objRequest.Method = "GET";
                objRequest.Timeout = 600000;


                //objResponse = objRequest.GetResponse();
                byte[] buffer = new byte[32768];
                using (Stream input = objRequest.GetResponse().GetResponseStream())
                {
                    using (FileStream output = new FileStream("C:\\temp\\ExportGroups.zip",
                    FileMode.CreateNew))
                    {
                        int bytesRead;

                        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            output.Write(buffer, 0, bytesRead);
                        }
                    }
                }
                Dts.TaskResult = (int)ScriptResults.Success;
            }
            catch (Exception e)
            {
                Dts.TaskResult = (int)ScriptResults.Failure;
            }

		}
 
Share this answer
 

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