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

发布于 2024-12-12 01:11:44 字数 381 浏览 0 评论 0原文

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

  1. 我可以通过递归遍历根文件夹来获取列表中的所有文件路径。目前,我可以压缩多个文件,但前提是它们位于单个文件夹中。

  2. 无法找到使用正确的文件夹层次结构和文件结构来压缩子文件夹并将其保存在独立存储中的方法。还需要使用正确的文件夹和文件结构对其进行解压缩。

I have problems in using SharpZipLib with isolated storage in WP7 to zip subfolders in isolated storage. My folder structure is like I'm having a rootFolder in isolated storage and inside that there is subFolder having some text files and more subfolders (contains .jpg and .png). I could go for Dotnetzip but I'm not sure it is available for WP7 or not and about its usage.

  1. I am able to get all the file pathes in a list by recursively traversing on root folder. At present I am able to zip multiple files but only when they are inside a single folder.

  2. Can't find way to zip subFolder with correct hierarchy of folder and file structure and save it inside isolated storage. Also needs to unzip it with correct folder and file structure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍暮颜 2024-12-19 01:11:44

您可以使用适用于 Silverlight/Windows Phone 7 的 SharpZipLib 来执行此操作。

以下代码基于 此示例并演示如何压缩根文件夹(包括子文件夹和文件)。

简短概述:

  • button1_Click 准备一些虚拟文件夹和文件以进行概念验证:一个包含一个文件的文件夹 root 和两个子文件夹,每个文件夹也包含一个文件,然后调用 CreateZip 压缩从 root 开始的整个目录树
  • CreateZip 准备 zip 文件并通过调用 开始递归文件夹压缩压缩文件夹
  • 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(@"root\subfolder1");
            isf.CreateDirectory(@"root\subfolder2");

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\file0.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("hello");
                stream.Write(bytes, 0, bytes.Length);
            }

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\subfolder1\file1.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("zip");
                stream.Write(bytes, 0, bytes.Length);
            }

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\subfolder2\file2.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("world");
                stream.Write(bytes, 0, bytes.Length);
            }

        }
        CreateZip("root.zip", null, "root");
    }

You can do this with SharpZipLib for Silverlight/Windows Phone 7.

The following code is based on this example and demonstrates how to zip a root folder including subfolders and files.

Short overview:

  • button1_Click prepares some dummy folders and files for proof of concept: a folder root containing a file and two subfolders each also containing a file, then it calls CreateZip to compress the whole directory tree starting with root
  • CreateZip prepares the zip file and starts recursive folder compression by calling CompressFolder
  • CompressFolder adds all files in a given dir to the zip file and recurses into subdirectories

The code:

    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(@"root\subfolder1");
            isf.CreateDirectory(@"root\subfolder2");

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\file0.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("hello");
                stream.Write(bytes, 0, bytes.Length);
            }

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\subfolder1\file1.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("zip");
                stream.Write(bytes, 0, bytes.Length);
            }

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"root\subfolder2\file2.txt", System.IO.FileMode.Create, isf))
            {
                byte[] bytes = Encoding.Unicode.GetBytes("world");
                stream.Write(bytes, 0, bytes.Length);
            }

        }
        CreateZip("root.zip", null, "root");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文