zoukankan      html  css  js  c++  java
  • 文件转二进制流拆分与组装


    //上传文件转二进制流
    string fileFullPath = Path.Combine(UploadRoot, filePath);
    using (FileStream fs = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    using (BinaryReader FileReader = new BinaryReader(fs))
    {
    byte[] Bytes;
    var file_size = 8192;
    int fileCount = Convert.ToInt32(Math.Ceiling((double)fs.Length / file_size));
    for (int i = 0; i < fileCount; i++)
    {
    Content Content = new Content();
    Bytes = FileReader.ReadBytes(file_size);
    Content.Id = Guid.NewGuid();
    Content.F_Content = Compress(Bytes);
    Content.F_OrderIndex = i;
    Content.CreateTime = DateTime.Now;
    _iContentAppService.Add(Content, null);
    }
    }
    }

    //组二进制流
    public byte[] GetContent(Guid id)
    {
    List<byte> byteSource = new List<byte>();
    try
    {
    var bAFJContents = bAFJContentAppService.FindByFilter(t => t.F_BAFJ_ID == id, null, null, f => new { f.Id });
    for (int pageNumber = 1; pageNumber <= bAFJContents.Count() / PageSize + 1; pageNumber++)
    {
    var BAFJContent = bAFJContentAppService.FindPagedByFilter(specification.SatisfiedBy(), null, pageNumber, PageSize, order, null);
    foreach (var item in BAFJContent)
    {
    byteSource.AddRange(Depress(item.F_Content));
    }
    }
    }
    catch (Exception ex)
    {
    LoggerFactory.CreateLog().Error(ex.ToString());
    }
    return byteSource.ToArray();
    }


    /// <summary>
    /// 压缩二进制流
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] data)
    {
    using (var memoryStream = new MemoryStream())
    {
    using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Compress))
    {
    compressionStream.Write(data, 0, data.Length);
    compressionStream.Flush();
    }
    return memoryStream.ToArray();
    }
    }

    /// <summary>
    /// 解压二进制流
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] Depress(byte[] data)
    {
    using (var memoryStream = new MemoryStream(data))
    using (var outStream = new MemoryStream())
    {
    using (var compressionStream = new GZipStream(memoryStream, CompressionMode.Decompress))
    {
    compressionStream.CopyTo(outStream);
    compressionStream.Flush();
    }
    return outStream.ToArray();
    }
    }

  • 相关阅读:
    NYOJ题目28大数阶乘
    网页小图标设置
    Sass中文乱码问题(手动编译和watch编译)
    设计模式之构建者模式(Builder):初步理解
    Struts2之类型转换器
    css设置网页文本选中样式
    由超市临时储物柜引发的一点设计随想...
    前端资源相关参考资料
    Struts2拦截器之ExceptionMappingInterceptor(异常映射拦截器)
    Struts2之OGNL
  • 原文地址:https://www.cnblogs.com/chengzi00/p/14448003.html
Copyright © 2011-2022 走看看