如何压缩和解压缩包含子文件夹和文件的独立存储中的根文件夹



我在使用WP7中具有隔离存储的SharpZipLib来压缩隔离存储中的子文件夹时遇到问题。我的文件夹结构就像我在隔离存储中有一个rootFolder,里面有子文件夹有一些文本文件和更多的子文件夹(包含。jpg和。png)。我可以选择Dotnetzip,但我不确定它是否适用于WP7,也不确定它的用法。

  1. 我能够通过递归遍历根文件夹来获得列表中的所有文件路径。目前,我能够压缩多个文件,但只有当他们在一个文件夹内。

  2. 无法找到具有正确文件夹和文件结构层次结构的压缩子文件夹并将其保存在隔离的存储中。还需要用正确的文件夹和文件结构解压缩

你可以在Silverlight/Windows Phone 7上使用SharpZipLib。

下面的代码基于这个示例,演示了如何压缩根文件夹,包括子文件夹和文件。

简短概述:

  • button1_Click准备了一些虚拟文件夹和文件来证明概念:一个文件夹包含一个文件,两个子文件夹每个也包含一个文件,然后调用CreateZip压缩从
  • 开始的整个目录树
  • CreateZip准备zip文件,通过调用CompressFolder
  • 开始递归文件夹压缩。
  • CompressFolder将指定目录下的所有文件添加到zip文件中,并递归到子目录
代码:
    using System.IO.IsolatedStorage;
    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Core;
    using System.Text;
    // Recurses down the folder structure
    //
    private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset, IsolatedStorageFile isf)
    {
        string[] files = isf.GetFileNames(System.IO.Path.Combine(path, "*.*"));
        foreach (string filename in files)
        {
            string filenameWithPath = System.IO.Path.Combine(path, filename);
            string entryName = filenameWithPath.Substring(folderOffset); // Makes the name in zip based on the folder
            entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = isf.GetLastWriteTime(filenameWithPath).DateTime; // Note the zip format stores 2 second granularity
            // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
            // you need to do one of the following: Specify UseZip64.Off, or set the Size.
            // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
            // but the zip will be in Zip64 format which not all utilities can understand.
            //   zipStream.UseZip64 = UseZip64.Off;
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filenameWithPath, System.IO.FileMode.Open, isf))
            {
                newEntry.Size = stream.Length;
            }
            zipStream.PutNextEntry(newEntry);
            // Zip the file in buffered chunks
            // the "using" will close the stream even if an exception occurs
            byte[] buffer = new byte[4096];
            using (IsolatedStorageFileStream streamReader = isf.OpenFile(filenameWithPath, System.IO.FileMode.Open))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();
        }
        string[] folders = isf.GetDirectoryNames(System.IO.Path.Combine(path, "*.*"));
        foreach (string folder in folders)
        {
            CompressFolder(System.IO.Path.Combine(path, folder), zipStream, folderOffset, isf);
        }
    }
    // Compresses the files in the nominated folder, and creates a zip file on disk named as outPathname.
    //
    public void CreateZip(string outPathname, string password, string folderName)
    {
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fsOut = new IsolatedStorageFileStream(outPathname, System.IO.FileMode.Create, isf))
            {
                ZipOutputStream zipStream = new ZipOutputStream(fsOut);
                zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
                zipStream.Password = password;  // optional. Null is the same as not setting.
                // This setting will strip the leading part of the folder path in the entries, to
                // make the entries relative to the starting folder.
                // To include the full path for each entry up to the drive root, assign folderOffset = 0.
                // int folderOffset = folderName.Length + (folderName.EndsWith("\") ? 0 : 1); // hu: currently not used for WP7 sample
                int folderOffset = 0;
                CompressFolder(folderName, zipStream, folderOffset, isf);
                zipStream.Close();
            }
        }
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            isf.CreateDirectory(@"root");
            isf.CreateDirectory(@"rootsubfolder1");
            isf.CreateDirectory(@"rootsubfolder2");
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"rootfile0.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("hello");
                stream.Write(bytes, 0, bytes.Length);
            }
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"rootsubfolder1file1.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("zip");
                stream.Write(bytes, 0, bytes.Length);
            }
            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"rootsubfolder2file2.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("world");
                stream.Write(bytes, 0, bytes.Length);
            }
        }
        CreateZip("root.zip", null, "root");
    }

最新更新