如何在不使用第三方 API 的情况下在 C# 中压缩文件? [英] How do I ZIP a file in C#, using no 3rd-party APIs?

查看:30
本文介绍了如何在不使用第三方 API 的情况下在 C# 中压缩文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这不是重复的,所以请耐心等待.

I'm pretty sure this is not a duplicate so bear with me for just a minute.

如何在不使用任何第三方库的情况下以编程方式 (C#) ZIP 文件(在 Windows 中)?我需要一个本机 Windows 调用或类似的东西;我真的不喜欢开始一个过程的想法,但如果我绝对需要,我会的.PInovke 调用会好得多.

How can I programatically (C#) ZIP a file (in Windows) without using any third party libraries? I need a native windows call or something like that; I really dislike the idea of starting a process, but I will if I absolutely have to. A PInovke call would be much better.

如果做不到这一点,让我告诉您我真正想要实现的目标:我需要能够让用户在单个请求中下载一组文档.关于如何实现这一点的任何想法?

Failing that, let me tell you what I'm really trying to accomplish: I need the ability to let a user download a collection of documents in a single request. Any ideas on how to accomplish this?

推荐答案

您在使用 .NET 3.5 吗?您可以使用 ZipPackage 类和相关类.它不仅仅是压缩文件列表,因为它需要您添加的每个文件的 MIME 类型.它可能会做你想做的事.

Are you using .NET 3.5? You could use the ZipPackage class and related classes. Its more than just zipping up a file list because it wants a MIME type for each file you add. It might do what you want.

我目前正在使用这些类解决类似的问题,将多个相关文件归档到一个文件中以供下载.我们使用文件扩展名将下载文件与我们的桌面应用程序相关联.我们遇到的一个小问题是,不可能只使用像 7-zip 这样的第三方工具来创建 zip 文件,因为客户端代码无法打开它——ZipPackage 添加了一个描述内容类型的隐藏文件每个组件文件,如果缺少该内容类型文件,则无法打开 zip 文件.

I'm currently using these classes for a similar problem to archive several related files into a single file for download. We use a file extension to associate the download file with our desktop app. One small problem we ran into was that its not possible to just use a third-party tool like 7-zip to create the zip files because the client side code can't open it -- ZipPackage adds a hidden file describing the content type of each component file and cannot open a zip file if that content type file is missing.

这篇关于如何在不使用第三方 API 的情况下在 C# 中压缩文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆