16

以下代码用于下载 zip 文件并在手机上解压缩。

以前在 WP7 上工作的相同代码,我开始在 WP8 设备上进行测试,奇怪的事情正在发生……现在它在 WP8 上工作,但不再WP7 上工作。

在 WP7 上它给出了一个错误

Wrong Local header signature: 0x6D74683C

有人可以告诉我这里有什么问题吗?

观察(发布问题后 2 天)

我有一些观察......在这里详细分享(图片格式)或(Excel格式

编码

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;

namespace iq_main.Network
{

    public class IQ_Download
    {
        private string zipFilePassword = String.Empty;
        private string fileNameToBeStoredAs = String.Empty;
        private string urlToBeDownloaded = String.Empty;
        private HttpWebResponse response;

        public void Download(string _urlToBeDownloaded = GlobalConstants.DownloadLanguageConfigurationUrl, string _fileNameToBeStoredAs = GlobalConstants.DownloadLanguageConfigurationXmlFilename, string _zipFilePassword = GlobalConstants.DownloadZipsPassword)
        {

            urlToBeDownloaded = _urlToBeDownloaded; 
            fileNameToBeStoredAs = _fileNameToBeStoredAs;
            zipFilePassword = _zipFilePassword;

            System.Uri targetUri = new System.Uri(urlToBeDownloaded);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);

            request.BeginGetResponse(new AsyncCallback(WebRequestCallBack), request);
        }


        void WebRequestCallBack(IAsyncResult result)
        {
            HttpWebRequest resultInfo = (HttpWebRequest)result.AsyncState;
            response = (HttpWebResponse)resultInfo.EndGetResponse(result);
            try
            {

                using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
                {
                    //open isolated storage to save files
                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (ZipInputStream s = new ZipInputStream(httpwebStreamReader.BaseStream))
                        {
                            if (zipFilePassword != String.Empty)
                                s.Password = zipFilePassword;//if archive is encrypted

                            ZipEntry theEntry;
                            try
                            {
//EXCEPTION OCCURS ON THE VERY NEXT LINE (while...)    
                                while ((theEntry = s.GetNextEntry()) != null)
                                {
                                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                                    string fileName = Path.GetFileName(theEntry.Name);
                                    fileName = fileNameToBeStoredAs;

                                    // create directory
                                    if (directoryName.Length > 0)
                                    {
                                        isoStore.CreateDirectory(directoryName);
                                        //Directory.CreateDirectory(directoryName);
                                    }

                                    if (fileName != String.Empty)
                                    {

                                        //save file to isolated storage
                                        using (BinaryWriter streamWriter =
                                                new BinaryWriter(new IsolatedStorageFileStream(theEntry.Name,
                                                FileMode.Create, FileAccess.Write, FileShare.Write, isoStore)))
                                        {

                                            int size = 2048;
                                            byte[] data = new byte[2048];
                                            while (true)
                                            {
                                                size = s.Read(data, 0, data.Length);
                                                if (size > 0)
                                                    streamWriter.Write(data, 0, size);
                                                else
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                            catch (ZipException ze)
                            {
                                Debug.WriteLine(ze.Message);
                            }
                        }
                    }
                }
            } //try
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }//WebRequestCallBack Method */
    } //Class ends
}

输出堆栈

    Step into: Stepping over method without symbols 'string.operator !='
    Step into: Stepping over method without symbols 'ICSharpCode.SharpZipLib.Zip.ZipInputStream.Password.set'
    Step into: Stepping over method without symbols 'string.operator !='
    Step into: Stepping over method without symbols 'ICSharpCode.SharpZipLib.Zip.ZipInputStream.Password.set'
    Step into: Stepping over method without symbols 'ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry'
    A first chance exception of type 'ICSharpCode.SharpZipLib.Zip.ZipException' occurred in SharpZipLib.WindowsPhone7.dll
    Step into: Stepping over method without symbols 'System.Exception.Message.get'
    Step into: Stepping over method without symbols 'System.Diagnostics.Debug.WriteLine'
    Wrong Local header signature: 0x6D74683C
    A first chance exception of type 'ICSharpCode.SharpZipLib.Zip.ZipException' occurred in SharpZipLib.WindowsPhone7.dll
    Wrong Local header signature: 0x6D74683C
4

3 回答 3

19

标头代码 0x6D74683C 对应于 ASCII 序列<htm,我认为它是网页中截断的 HTML 标头。如果您正在下载 .zip 存档的内容,那么这可能意味着 Web 服务器正在返回 HTML 代码而不是预期的存档(错误页面或类似的东西)。也许您应该在将流提供给 ICSharpCode.SharpZipLib 之前检查 HTTP Content-Type 标头。

于 2013-05-06T16:11:54.533 回答
4

当您使用 WP7 时,您会从 Dropbox 收到一个 html:

成立

该资源在 {"your new link here} 找到;您应该被自动重定向。-------------------------------------------- ----- WSGI 服务器

在 Wp8 中,此重定向自动工作,但在 Wp7 中,此重定向不起作用。

我认为适合您的解决方案:只需将链接更改为新的(您可以在收到的 html 文件中找到它)

于 2013-05-07T11:27:32.330 回答
1

问题与他们的答案中解释的“Leandro Taset”和“d.lavysh”相同。但是,WP7 为何会添加 HTML 标头仍是未知数?

无论如何,修改后的代码现在适用于 WP7 和 WP8 设备。此代码还能够从 Web 托管服务或 DropBox 下载文件。

我上面贴的代码差不多,只是修改了Download方法,修改后是这样的:

    public async void Download(string _urlToBeDownloaded = GlobalConstants.DownloadLanguageConfigurationUrl, string _fileNameToBeStoredAs = GlobalConstants.DownloadLanguageConfigurationXmlFilename, string _zipFilePassword = GlobalConstants.DownloadZipsPassword)
    {
        //The following IF block is addition to the code above. 
        //Here the headers are checked and if "WP7" and the URL is pointing to  the "Dropbox", the inner URL is fetched out of the headers.
        if (GlobalVariables.IsWP7 && _urlToBeDownloaded.ToLower().Contains("dropbox"))
        {

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_urlToBeDownloaded);
            HttpWebResponse webResponse = await webRequest.GetResponseAsync() as HttpWebResponse;

            for (int i = 0; i < webResponse.Headers.Count; ++i)
            {
                if (webResponse.Headers.AllKeys[i].ToLower() == "location")
                {
                    _urlToBeDownloaded = webResponse.Headers["location"] ;
                    break;
                }
            }
        }

        urlToBeDownloaded = _urlToBeDownloaded ;
        fileNameToBeStoredAs = _fileNameToBeStoredAs;
        zipFilePassword = _zipFilePassword;


        System.Uri targetUri = new System.Uri(urlToBeDownloaded);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);

        request.BeginGetResponse(new AsyncCallback(WebRequestCallBack), request);
    }
于 2013-05-14T17:07:14.180 回答