以下代码用于下载zip文件并在手机上解压缩。
用于WP7的相同代码,我开始在WP8设备上测试,奇怪的事情正在发生...现在它适用于WP8但在WP7上 NOT 了。
在WP7上,它提供了 ERROR :
Wrong Local header signature: 0x6D74683C
有人能告诉我这里有什么问题吗?
观察(发布问题后2天)
我有一些观察......详细分享(Image format)或(Excel format)
代码
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
答案 0 :(得分:15)
标题代码0x6D74683C对应于ASCII序列<htm
,我认为它是网页中截断的HTML标题。如果您正在下载.zip存档的内容,那么这可能意味着Web服务器正在返回HTML代码而不是预期的存档(错误页面或类似的东西)。也许您应该在将流提供给ICSharpCode.SharpZipLib之前检查HTTP Content-Type标头。
答案 1 :(得分:4)
当你使用WP7时,你会从dropbox收到一个html:
实测值
该资源位于{“您的新链接}”;您应该是 自动重定向。 ------------------------------------ WSGI Server
在Wp8中,ithis重定向会自动生效,但在Wp7中,此重定向不起作用。
我认为适合您的解决方案:只需更改新链接(您可以在收到的html文件中找到它)
答案 2 :(得分: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);
}