[英]How to unzip a file in IsolatedStorage in windows phone 8 app?
在我的應用程序內,我試圖一次下載大約180個小音頻文件。 我嘗試了BackgroundTransferService,但是這么多小文件看起來並不穩定。 所以,現在我正在下載所有這些音頻的ZIP,並希望在“audio”文件夾中提取它們。 我在這個帖子中嘗試了這個方法:
但我收到此錯誤: 'System.IO.IOException' occurred in mscorlib.ni.dll...
中'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;
}
}
使用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;
}
}
}
}
}
}
有幾個第三方庫,以便在WP8中提取ZIP文件,如ZipLib,您可以從@ http://slsharpziplib.codeplex.com/下載,但DotNetZip是一個父ZipLib,更穩定。 這是一個示例代碼。 沒有檢查它是否有效,但這是你如何去做的。
ZipFile zip = ZipFile.Read(ZipFileToUnzip);
foreach (ZipEntry ent in zip)
{
ent.Extract(DirectoryWhereToUnizp, ExtractExistingFileAction.OverwriteSilently);
}
我剛剛解決了這個問題。 您可以做的是使用此方法,您的文件將保存到zip文件中存在的正確文件夾結構中的獨立存儲。 您可以根據需要更改要存儲數據的位置。
我剛剛讀了一個sample.zip文件。 從您的應用程序文件夾
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));
}
}
}
}
}
歡呼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.