无法读取为zipfile。使用C#? [英] Cannot read that as a zipfile. Using C#?

查看:198
本文介绍了无法读取为zipfile。使用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有两个文件,一个zip文件和其他文本文件,但是当我们下载时会收到错误: -

当我解压缩文件所以它收到错误: -

无法读取它作为ZipFile

我想从下载所有文件ftp并解压缩只有zip文件。



此行错误: -

Hello,

I have two files one zip file and other text file but when we are downloading so its getting error:-
when i unzip file so its getting error:-
"Cannot read that as a ZipFile."
I want to download all files from ftp and unzip only zip file.

Error in this line:-

string zipToUnpack = @"C:\" + file;
string unpackDirectory = @"C:\";
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
    foreach (ZipEntry ze in zip1)
    {
        ze.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
    }
}







此代码已成功运行只有一个zip文件。

只有一个文件解压缩,但不止一个文件没有下载zip和文本文件,它收到错误无法读取它作为ZipFile。



请帮帮我。

提前致谢。



Ankit Agarwal

软件工程师



我的尝试:






This code is running successfully with only one zip file.
Only one file is unzipping but more than one are not downloading zip and text file its getting error "Cannot read that as a ZipFile".

Please help me.
Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] files = ReadFileList();
            FTPSettings.IP = "xxxxxxxx/Test";
            FTPSettings.UserID = "xxxx";
            FTPSettings.Password = "xxxx";
            //FtpWebRequest reqFTP = null;
            //Stream ftpStream = null;
            foreach (string file in files)
            {
                //string fileName = e.Argument.ToString();

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
                request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
                request.Method = WebRequestMethods.Ftp.GetFileSize;
                request.Proxy = null;

                long fileSize; // this is the key for ReportProgress
                using (WebResponse resp = request.GetResponse())
                    fileSize = resp.ContentLength;

                request = (FtpWebRequest)WebRequest.Create("ftp://" + FTPSettings.IP + "/" + file);
                request.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
                using (Stream responseStream = responseFileDownload.GetResponseStream())
                using (FileStream writeStream = new FileStream(@"C:\" + file, FileMode.Create))
                {

                    int Length = 2048;
                    Byte[] buffer = new Byte[Length];
                    int bytesRead = responseStream.Read(buffer, 0, Length);
                    int bytes = 0;

                    while (bytesRead > 0)
                    {
                        writeStream.Write(buffer, 0, bytesRead);
                        bytesRead = responseStream.Read(buffer, 0, Length);
                        bytes += bytesRead;// don't forget to increment bytesRead !
                        int iProgress = 0;
                        int totalSize = (int)(fileSize) / 1000; // Kbytes
                        if (totalSize > 0)
                        {
                            iProgress = (bytes / 1000) * 100 / totalSize;
                        }
                        backgroundWorker1.ReportProgress(iProgress, totalSize);
                    }
                }
string zipToUnpack = @"C:\" + file;
                string unpackDirectory = @"C:\";
                using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
                {
                    foreach (ZipEntry ze in zip1)
                    {
                        ze.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
                    }
                }
}

public string[] ReadFileList()
        {
            //Debugger.Break();
            string[] mydownloadFiles;
            StringBuilder myresult = new StringBuilder();
            //WebResponse myresponse = null;
            StreamReader myreader = null;
            FtpWebRequest myreqFTP = null;
            try
            {

                FTPSettings.IP = "xxxxxxx/Test";
                FTPSettings.UserID = "xxxx";
                FTPSettings.Password = "xxxx";
                myreqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPSettings.IP + "/"));
                myreqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                myreqFTP.UseBinary = true;

                myreqFTP.Credentials = new NetworkCredential(FTPSettings.UserID, FTPSettings.Password);
                FtpWebResponse response = (FtpWebResponse)myreqFTP.GetResponse();
                myreader = new StreamReader(response.GetResponseStream());
                string myline = myreader.ReadLine();
                while (myline != null)
                {
                    myresult.Append(myline);
                    myresult.Append("\n");
                    myline = myreader.ReadLine();
                }
                // to remove the trailing '\n'
                myresult.Remove(myresult.ToString().LastIndexOf('\n'), 1);
                return myresult.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                if (myreader != null)
                {
                    myreader.Close();
                }
                mydownloadFiles = null;
                return mydownloadFiles;
            }
        }



public static class FTPSettings
        {
            public static string IP { get; set; }
            public static string UserID { get; set; }
            public static string Password { get; set; }
        }

推荐答案

在使用TotalSize变量进行除法之前检查它的值。我会把它写成if语句

Check the value of your TotalSize variable before using it to divide. I would put it into an if statement
iProgress int = 0;
if(TotalSize > 0 ){iProgress = (bytes / 1000) * 100 / totalSize;}

backgroundWorker1.ReportProgress(iProgress, totalSize);





由于类型转换,TotalSize可能为0,因为除数将给出小数,你的()在错误的地方



TotalSize may be 0 because of a type conversion as divide will give a decimal, your () are in the wrong place


如果你在这段代码中得到Divide By Zero异常:

If you are getting a Divide By Zero exception in this code:
bytes += bytesRead;// don't forget to increment bytesRead !
int totalSize = (int)(fileSize) / 1000; // Kbytes
backgroundWorker1.ReportProgress((bytes / 1000) * 100 / totalSize, totalSize);

那么问题必须是 totalSize 在最后一行为零:这意味着 fileSize 小于1000.



我们无法解决这个问题:我们无法让文件更大!您需要使用调试器检查fileSize的位置,并获得更好的值,或者(如果它是正确的且低于1000)测试它并且根本不进行除法。

Then the problem has to be that totalSize is zero in the final line: which means that the fileSize is less than 1000.

We can't fix that for you: we can't make files bigger! You need to check where you get fileSize from using the debugger and either get a better value into it, or (if it is correct and below 1000) test for it and not do the divide at all.


这篇关于无法读取为zipfile。使用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆