Esempio n. 1
0
        private byte[] Unpack(YlFilePackage pack)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            #region write head info

            //write file count
            short count = (short)pack.Files.Count;

            bw.Write(count);

            foreach (YlFileInfo info in pack.Files)
            {
                bw.Write(info.FileNameIndex); //4字节:图片文件名起始位置
                bw.Write((short)StringToUnicodeBytes(info.File.FileName).Length); //2字节:文件名长度
                bw.Write(info.FileContentIndex);    //4字节:图片内容起始位置
                bw.Write(info.File.FileLength);     //4字节:图片文件长度
            }

            #endregion

            #region write content

            foreach (YlFileInfo info in pack.Files)
            {
                bw.Write(StringToUnicodeBytes(info.File.FileName));
                bw.Write(info.File.Content);
            }

            #endregion

            bw.Flush();
            bw.Close();

            ms.Close();

            return ms.ToArray();
        }
Esempio n. 2
0
        private YlFilePackage Pack(List<YlFile> files, List<string> fileIds)
        {
            YlFilePackage pkg = new YlFilePackage();

            //create head info

            int fileCount = files.Count;

            int pos = 0;

            int headLength = 0;

            pkg.Files = new List<YlFileInfo>();

            foreach (YlFile file in files)
            {
                YlFileInfo info = new YlFileInfo();

                info.File = file;

                //4字节:图片文件名起始位置
                //2字节:文件名长度
                //4字节:图片内容起始位置
                //4字节:图片文件长度

                headLength += 4 * 3 + 2;

                pkg.Files.Add(info);
            }

            pos += 2 + headLength; //文件数长度 + 头长度

            pkg.HeadLength = pos;

            //update filename index
            foreach (YlFileInfo info in pkg.Files)
            {
                info.FileNameIndex = pos;

                byte[] fnBytes = StringToUnicodeBytes(info.File.FileName);

                info.FileNameIndex = pos;
                pos += fnBytes.Length;

                info.FileContentIndex = pos;
                pos += info.File.FileLength;

            }

            pkg.FileLength = pos;

            return pkg;
        }