如何在 Silverlight 中压缩和解压缩文件夹及其子文件夹?

发布于 2024-12-12 12:19:14 字数 2262 浏览 0 评论 0原文

我有一个 Windows Phone 应用程序。我正在使用 SharpZipLib 来压缩文件夹及其子文件夹。这仅压缩文件夹,但不会压缩文件夹内的数据。谁能指导我如何做到这一点?

我的代码:

private void btnZip_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            foreach (string filename in appStore.GetFileNames(directoryName + "/" + "*.txt"))
            {                   
               GetCompressedByteArray(filename);
            }
            textBlock2.Text = "Created file has Zipped Successfully";
        }
    }
 public byte[] GetCompressedByteArray(string content)
        {
            byte[] compressedResult;
            using (MemoryStream zippedMemoryStream = new MemoryStream())
            {
                using (ZipOutputStream zipOutputStream = new ZipOutputStream(zippedMemoryStream))
                {
                    zipOutputStream.SetLevel(9);
                    byte[] buffer;                   
                    using (MemoryStream file = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        buffer = new byte[file.Length];
                        file.Read(buffer, 0, buffer.Length);
                    }                    
                    ZipEntry entry = new ZipEntry(content);
                    zipOutputStream.PutNextEntry(entry);
                    zipOutputStream.Write(buffer, 0, buffer.Length);
                    zipOutputStream.Finish();
                }
                compressedResult = zippedMemoryStream.ToArray();
            }
            WriteToIsolatedStorage(compressedResult);
            return compressedResult;
        }

        public void WriteToIsolatedStorage(byte[] compressedBytes)
        {
            IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
            appStore.CreateDirectory(ZipFolder);
            using (IsolatedStorageFileStream zipTemplateStream = new IsolatedStorageFileStream(ZipFolder+"/"+directoryName + ".zip", FileMode.OpenOrCreate, appStore))
            using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream))
            {
                streamWriter.Write(compressedBytes);
            }
        }

I have a Windows Phone application. I am using SharpZipLib to zip folders and its sub folders. This is zipping only the folder but the data inside the folders is not getting zipped. Can anyone guide me how to do this?

My code:

private void btnZip_Click(object sender, RoutedEventArgs e)
    {
        using (IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication())
        {
            foreach (string filename in appStore.GetFileNames(directoryName + "/" + "*.txt"))
            {                   
               GetCompressedByteArray(filename);
            }
            textBlock2.Text = "Created file has Zipped Successfully";
        }
    }
 public byte[] GetCompressedByteArray(string content)
        {
            byte[] compressedResult;
            using (MemoryStream zippedMemoryStream = new MemoryStream())
            {
                using (ZipOutputStream zipOutputStream = new ZipOutputStream(zippedMemoryStream))
                {
                    zipOutputStream.SetLevel(9);
                    byte[] buffer;                   
                    using (MemoryStream file = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        buffer = new byte[file.Length];
                        file.Read(buffer, 0, buffer.Length);
                    }                    
                    ZipEntry entry = new ZipEntry(content);
                    zipOutputStream.PutNextEntry(entry);
                    zipOutputStream.Write(buffer, 0, buffer.Length);
                    zipOutputStream.Finish();
                }
                compressedResult = zippedMemoryStream.ToArray();
            }
            WriteToIsolatedStorage(compressedResult);
            return compressedResult;
        }

        public void WriteToIsolatedStorage(byte[] compressedBytes)
        {
            IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication();
            appStore.CreateDirectory(ZipFolder);
            using (IsolatedStorageFileStream zipTemplateStream = new IsolatedStorageFileStream(ZipFolder+"/"+directoryName + ".zip", FileMode.OpenOrCreate, appStore))
            using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream))
            {
                streamWriter.Write(compressedBytes);
            }
        }

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

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

发布评论

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

评论(2

你好,陌生人 2024-12-19 12:19:14

我想你会发现本指南很有帮助。

上面链接的摘录

ZipFile 对象提供了一个名为 AddDirectory() 的方法,该方法
接受参数directoryName。这种方法的问题是
它不会在指定目录中添加文件,但是
相反,只是在 zip 文件中创建一个目录。为了使这个
工作,您需要通过循环来获取该目录内的文件
该目录中的所有对象并一次添加一个。我是
能够通过创建一个递归函数来完成此任务
钻取您想要的文件夹的整个目录结构
拉链。下面是该函数的片段。

我想您也面临同样的问题,即文件夹已添加到 zip 文件,但内容和子文件夹未压缩。

希望这有帮助。

I think you'll find this guide helpful.

An excerpt from the above link

The ZipFile object provides a method called AddDirectory() that
accepts a parameter directoryName. The problem with this method is
that it doesn't add the files inside the specified directory but
instead just creates a directory inside the zip file. To make this
work, you need to get the files inside that directory by looping thru
all objects in that directory and adding them one at a time. I was
able to accomplish this task by creating a recursive function that
drills through the whole directory structure of the folder you want to
zip. Below is a snippet of the function.

I guess you too are facing the same problem where the folder is added to the zip file, but the contents and sub folders are not zipped.

Hope this helps.

浮世清欢 2024-12-19 12:19:14

看看这里有关如何使用 SharpZipLib 压缩根文件夹(包括嵌套文件夹)的代码示例。

Have a look over here for a code sample on how to use SharpZipLib to zip a root folder including nested folders.

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