当前位置:网站首页>C#日常开发随手记------获取华为云桶中ZIP内的所有文件(包含System.NotSupportedException:“此流不支持查找操作“解决方法)
C#日常开发随手记------获取华为云桶中ZIP内的所有文件(包含System.NotSupportedException:“此流不支持查找操作“解决方法)
2022-04-22 06:10:00 【焦糖丨玛奇朵】
需求:根据华为云桶的文件下载地址,展示出目标ZIP文件内的所有文件信息
/// <summary>
/// 获取目标ZIP文件内的所有文件信息
/// </summary>
/// <param name="stream">文件流</param>
/// <param name="fileList">返回的结果集</param>
/// <returns></returns>
private IList<fileInfo> GetFileInfoForZip(Stream stream,out IList<fileInfo> fileList)
{
//声明返回的结果集
fileList = new List<fileInfo>();
stream.Position = 0;
var factory = ArchiveFactory.Open(stream);
foreach (var item in factory.Entries)
{
fileInfo fil = new fileInfo();
var memoryStream = new MemoryStream();
var entryStream = item.OpenEntryStream();
//entryStream.Position = 0;
if (entryStream != null)
{
entryStream.CopyTo(memoryStream);
entryStream.Dispose();
}
fil.Name = item.Key;
fil.size = item.Size.ToString();
fil.Time = item.LastModifiedTime.ToString();
fileList.Add(fil);
}
return fileList;
}
/// <summary>
/// 流转换方法----直接转Stream流会出现 System.NotSupportedException:"此流不支持查找操作" 错误
/// </summary>
/// <param name="instream"></param>
/// <returns></returns>
MemoryStream StreamToMemoryStream(Stream instream)
{
MemoryStream outstream = new MemoryStream();
const int bufferLength = 4096;
byte[] bl = new byte[bufferLength];
int count = 0;
while ((count = instream.Read(bl, 0, bufferLength)) > 0)
{
outstream.Write(bl, 0, count);
}
return outstream;
}
/// <summary>
/// 用到的文件信息类
/// </summary>
public class fileInfo
{
/// <summary>
/// 文件名
/// </summary>
public string Name;
/// <summary>
/// 文件大小
/// </summary>
public string size;
/// <summary>
/// 创建时间
/// </summary>
public string Time;
}
//按钮的触发事件
protected void btn_Text_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream rs = response.GetResponseStream();
var ms = StreamToMemoryStream(rs);//rs.Length 此流不支持查找,先转为MemoryStream
IList<fileInfo> fileList = new List<fileInfo>();
GetFileInfoForZip(ms, out fileList);
}
}
**
需求:根据本地地址获取目标Zip压缩包中的文件信息
**
/// <summary>
/// 根据附件地址获取附件中文件的信息
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileList"></param>
/// <returns></returns>
public IList<fileInfo> getZipInfo(string filePath, out IList<fileInfo> fileList)
{
//根据ItemId和itemFileId查询对应的文件信息(默认智能查询到一件,itemFileId为主键理应不会重复)
fileList = new List<fileInfo>();
//判断是否查询到数据
var factory = ArchiveFactory.Open(filePath);
foreach (var fileInfo in factory.Entries)
{
fileInfo fil = new fileInfo();
fil.Name = fileInfo.Key;
fil.size = fileInfo.Size.ToString();
fil.Time = fileInfo.LastModifiedTime.ToString();
fileList.Add(fil);
}
return fileList;
}
/// <summary>
/// 用到的文件信息类
/// </summary>
public class fileInfo
{
/// <summary>
/// 文件名
/// </summary>
public string Name;
/// <summary>
/// 文件大小
/// </summary>
public string size;
/// <summary>
/// 创建时间
/// </summary>
public string Time;
}
版权声明
本文为[焦糖丨玛奇朵]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_37739112/article/details/124041542
边栏推荐
- A solution to the error of selecting objects in pyautocad
- STM32 timer ocref output configuration timx - > CCER
- RT thread transplantation BSP driven UART
- STM32学习记录0004——ISP串口下载
- Ma8608 Qiyan USB 2.0 High Speed 4-port USB hub controller chip scheme
- C skill tree evaluation - 0415 experience diary
- VScode 看这一篇就够了
- Design parameters and circuit comparison of type C to HDMI 4k30hz expansion chip scheme cs5261 and cs5266
- CAN光端机在消防水炮中的应用
- 微电子专业是做芯片的吗?芯片和什么专业有关?
猜你喜欢

Mcs-5 interrupt technology (Theory)

Alternative ps8625 | alternative ps8622 | design and development of cost-effective EDP to LVDS adapter board cs5211

Application of mcicecan in industrial computer

Lcfnet series can to optical fiber Ethernet equipment realizes ultra long-distance optical fiber communication between can networks

机器人系统中CAN卡的应用

Td041s485h is fully compatible with iso3080, iso3086, iso3082 and iso3088

Ag9310mcq supports the design reference circuit of mother seat forward and reverse plug typec to HDMI projection scheme

疫情环境下工程机械中的透传云网关远程程序升级方案

Design of second-order RC low-pass filter for PWM wave to DC

STM32 timer ocref output configuration timx - > CCER
随机推荐
在消防联网(楼宇、工厂、海上风电、管廊等)中CAN光纤转换器、CAN总线光端机典型应用案例
6. What is ROS
DP转HDMI方案|CS5216方案应用|CS5216设计方案
替代 FE1.1s HUB讀卡主控芯片-MA8601
Ma8608 Qiyan USB 2.0 High Speed 4-port USB hub controller chip scheme
微信支付 iframe子页面 无响应
Design parameters and circuit comparison of type C to HDMI 4k30hz expansion chip scheme cs5261 and cs5266
Application of can optical transceiver in fire networking
软件测试流程
Replace rtd2171u | cs5266 design circuit | type C to HDMI scheme | cs5266an
Covariance and covariance matrix
CAN透传记录云网关为工程机械赋能
STM32 learning record 0004 - ISP serial port download
Oracle JDK vs OpenJDK
First order digital low-pass filter - C language / Matlab implementation
Li Kou 299: number guessing game
Application of can optical fiber optical transceiver in photoelectric slip ring
common-net hp unix系统 ftp服务器 listFiles 返回为空解决方案。
分布式任务调度与计算框架:PowerJob 快速开始(本地IDE版) 02
mysql的zip安装教程