使用zlib对swf压缩文件进行解压缩--学习纪事

来源:百度文库 编辑:神马文学网 时间:2024/05/24 05:20:44


                                      

在flash6.0以后的版本中,输出文件时,都提供了压缩这一选项,如果要在程序中获得flash的内容,必须对flash进行解压缩。它使用的压缩方法是zlib开放源码的压缩方法。从第9个字节开始到文件末尾全部进行了压缩,对其解压缩也是非常简单,可以使用zlib1.dll中提供的uncompress对其进行解压缩。

具体使用方法,参见zlib的官方网站:http://www.zlib.net

里面惟一需要注意的是:压缩和解压缩的缓冲区的大小,应该是实际内容大小的 1.01倍+12 个字节。否则就可能出现错误。

基本代码如下:

  CFile tmpfile;


  tmpfile.Open(filepath,CFile::modeRead);

  DWORD complen=tmpfile.GetLength();

  BYTE* header=(BYTE*)::GlobalAlloc(GPTR,8);


  BYTE* buf=(BYTE*)::GlobalAlloc(GPTR,(complen-8)*1.01+12);


  BYTE* bufd=(BYTE*)::GlobalAlloc(GPTR,(filelen-8)*1.01+12);

  tmpfile.Read(header,8);


  tmpfile.ReadHuge(buf,complen-8);

  tmpfile.Close();

  unsigned long destlen;//return the value of the destlen.

  uncompress(bufd,&destlen,buf,(complen-8)*1.01+12);

 //uncompress the swf file and return the destination file length.

  header[0]=‘F‘;//change the header indentifier.

 CString temp1,temp2;

  temp1=filepath.Left(filepath.GetLength()-4);

  temp2.Format("~%s$.swf",temp1);

  CFile filedes;

  filedes.Open(temp2,CFile::modeCreate|CFile::modeWrite);

  filedes.Write(header,8);

  filedes.Write(bufd,destlen);

  filedes.Close();

  ::GlobalFree(buf);

  ::GlobalFree(bufd);

  return temp2;

 

 

 

- 作者: ar4ever 2006年10月7日, 星期六 20:14