在我的应用程序中,我试图一次下载大约180个小音频文件。我尝试了BackgroundTransferService,但是这么多小文件看起来并不稳定。所以,现在我正在下载所有这些音频的ZIP,并希望在" audio"中提取它们。夹。我在这个帖子中尝试了这个方法:
How to unzip files in Windows Phone 8
但是我在以下代码中收到此错误:'System.IO.IOException' occurred in mscorlib.ni.dll...
。我怎样才能克服这个问题?
while (reader.ReadInt32() != 101010256)
{
reader.BaseStream.Seek(-5, SeekOrigin.Current); // this line causes error
}...
此外,我需要在哪里放置此代码以及在何处为其提供目标目录?
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(@"audio.rar", FileMode.Open, FileAccess.ReadWrite))
{
UnZipper unzip = new UnZipper(fileStream);
foreach (string filename in unzip.FileNamesInZip())
{
string FileName = filename;
}
}
答案 0 :(得分:0)
使用Silverlight SharpZipLib。将SharpZipLib.WindowsPhone7.dll添加到您的项目中(也适用于WP8 silverlight)。
private void Unzip()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
ZipEntry entry;
int size;
byte[] data = new byte[2048];
using (ZipInputStream zip = new ZipInputStream(store.OpenFile("YourZipFile.zip", FileMode.Open)))
{
// retrieve each file/folder
while ((entry = zip.GetNextEntry()) != null)
{
if (!entry.IsFile)
continue;
// if file name is music/rock/new.mp3, we need only new.mp3
// also you must make sure file name doesn't have unsupported chars like /,\,etc.
int index = entry.Name.LastIndexOf('/');
string name = entry.Name.Substring(index + 1);
// create file in isolated storage
using (var writer = store.OpenFile(name, FileMode.Create))
{
while (true)
{
size = zip.Read(data, 0, data.Length);
if (size > 0)
writer.Write(data, 0, size);
else
break;
}
}
}
}
}
}
答案 1 :(得分:0)
有几个第三方库可以在WP8中提取ZIP文件,例如ZipLib,您可以从@ http://slsharpziplib.codeplex.com/下载 但DotNetZip是一个父ZipLib并且更加稳定。 这是一个示例代码。没有检查它是否有效,但这就是你的方法。
ZipFile zip = ZipFile.Read(ZipFileToUnzip);
foreach (ZipEntry ent in zip)
{
ent.Extract(DirectoryWhereToUnizp, ExtractExistingFileAction.OverwriteSilently);
}
答案 2 :(得分:0)
我刚刚解决了这个问题。您可以做的是使用此方法,您的文件将保存到zip文件中存在的正确文件夹结构中的独立存储。您可以根据需要更改要存储数据的位置。
我刚读了一个sample.zip文件。从你的app文件夹。
private async Task UnZipFile()
{
var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (var fileStream = Application.GetResourceStream(new Uri("sample.zip", UriKind.Relative)).Stream)
{
var unzip = new UnZipper(fileStream);
foreach (string filename in unzip.FileNamesInZip)
{
if (!string.IsNullOrEmpty(filename))
{
if (filename.Any(m => m.Equals('/')))
{
myIsolatedStorage.CreateDirectory(filename.Substring(0, filename.LastIndexOfAny(new char[] { '/' })));
}
//save file entry to storage
using (var streamWriter =
new StreamWriter(new IsolatedStorageFileStream(filename,
FileMode.Create,
FileAccess.ReadWrite,
myIsolatedStorage)))
{
streamWriter.Write(unzip.GetFileStream(filename));
}
}
}
}
}
欢呼:)