生成的System.IO.Packaging.Package包文件(zip格式的)——test.fcmk(后缀名自己定义的),里面包含几个文件,可以通过修改后缀名为.zip,再用解压缩工具解压,就可以看到了里面的文件,但我不想被别人查看,就是不让别人解压包文件,如何实现?请高手帮忙!

解决方案 »

  1.   

    希望有demo,光说我哪会啊
    还有是,我是想实现excel.xlsx的在另存为时,可以选择加密保存这个功能,请了解这方面的详细点说,最好有个demo,谢谢
      

  2.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;

    public partial class day19_FileStreamDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 读取文件的二进制数据
        /// </summary>
        /// <param name="fileName">文件的完整路径</param>
        /// <returns></returns>
        protected byte[] ReadBinaryData(string fileName)
        {
            //声明一个数组来保存文件的二进制数据
            byte[] bytes = null;
            if (File.Exists(fileName))
            {
                //实例化FileInfo
                FileInfo fileInfo = new FileInfo(fileName);
                //根据FileInfo创建FileStream的实例
                FileStream fileStream=fileInfo.Open(FileMode.Open,FileAccess.Read);
                //实例化数组来保存文件的二进制内容
                bytes = new byte[fileInfo.Length];
                //将文件的二进制内容一次性读入到bytes这个数组中
                fileStream.Read(bytes, 0, bytes.Length);
                //关闭流对象
                fileStream.Close();
            }
            return bytes;
        }
        /// <summary>
        /// 将字节数组中的数据进行对称交换
        /// </summary>
        /// <param name="bytes">要进行对称交换的字节数组</param>
        /// <returns></returns>
        protected byte[] ExchangeByte(byte[] bytes)
        {
            //用于存放对称交换后的数据的数组
            byte[] result = null;
            if (bytes!= null)
            {
                int length=bytes.Length;
                result = new byte[length];
                //对称交换算法
                for (int i = 0; i <= (length + 1) / 2; i++)
                {
                    result[i] = bytes[length - i-1];
                    result[length - i-1] = bytes[i];
                }
            }
            return result;
        }
        
        protected void btnCreateFile_Click(object sender, EventArgs e)
        {
            //注意下面方式使用FileStream来实现文件拷贝
            //如果仅仅是拷贝文件可以直接使用File类或者FileInfo类中的方法
            //这里主要是演示FileStream的用法
            string path = Server.MapPath("~/images/zhoufoxcn2.jpg");
            //调用自定义的方法将文件的二进制内容读入到字节数组中
            byte[] bytes = ReadBinaryData(path);
            if (bytes != null)
            {
                //设置文件的新保存路径
                string newPath = Server.MapPath("~/day19/zhoufoxcn2.jpg");
                //创建文件流
                FileStream fileStream = File.Open(newPath, FileMode.Create, FileAccess.ReadWrite);
                //一次性将文件数据写入到文件中
                fileStream.Write(bytes, 0, bytes.Length);
                //关闭文件流
                fileStream.Close();
            }
        }
        protected void btnExchangeData_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("~/day19/zhoufoxcn2.jpg");
            byte[] bytes = ReadBinaryData(path);
            if (bytes != null)
            {
                //将文件的二进制数据对称交换
                Byte[] result = ExchangeByte(bytes);
                //设置对称交换后的二进制数据的保存路径
                string newPath = Server.MapPath("~/day19/zhoufoxcn2-Exchange.jpg");
                //创建文件流
                FileStream fileStream = File.Open(newPath, FileMode.Create, FileAccess.ReadWrite);
                //一次性将文件数据写入到文件中
                fileStream.Write(result, 0, result.Length);
                //关闭文件流
                fileStream.Close();
            }
        }
        protected void btnRevertData_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath("~/day19/zhoufoxcn2-Exchange.jpg");
            byte[] bytes = ReadBinaryData(path);
            if (bytes != null)
            {
                //将文件的二进制数据对称交换
                Byte[] result = ExchangeByte(bytes);
                //设置对称交换后的二进制数据的保存路径
                string newPath = Server.MapPath("~/day19/zhoufoxcn2-Revert.jpg");
                //创建文件流
                FileStream fileStream = File.Open(newPath, FileMode.Create, FileAccess.ReadWrite);
                //一次性将文件数据写入到文件中
                fileStream.Write(result, 0, result.Length);
                //关闭文件流
                fileStream.Close();
            }
        }
    }
      

  3.   

    我的需求也许没有说详细了,包文件的结构是不能变的(里面有许多的xml文件的),否则我在读取包的时候还要判断(这样不行,读取包的代码是不能改的)
    我的想法是,可以设置是否可以解压(给包文件加个壳或属性什么的):
    1、设置为直接可以解压,就可以解压
    2、设置为用密码可以解压,则输入密码可以解压
    3、设置为不可以解压,则不可以解压