解壓縮,壓縮和讀取flash標頭檔資訊

來源:互聯網
上載者:User
swf檔案在flash6以後多了壓縮的選項,使用zlib壓縮,有人寫的zlib的連結庫(http://zlibnetwrapper.sourceforge.net/),在他們的網頁裡面有詳細的使用協助.

下面是我寫的解壓縮,壓縮和讀取flash標頭檔資訊的代碼:

壓縮:
openFileDialog1.Filter = "JPG Files|*.swf";
openFileDialog1.Title = "Select a Swf File";

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = Path.GetFullPath(openFileDialog1.FileName);

 FileStream fs1 = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 BinaryReader r = new BinaryReader(fs1);
 byte[] writedata = r.ReadBytes((int)(fs1.Length - 1));
   
    
  
 ManagedZLib.ManagedZLib.Initialize();

string tempfile = "aaa.swf";
 FileStream fs = new FileStream(tempfile, FileMode.OpenOrCreate);

 byte[] writefiledata = new byte[8];
 writefiledata[0] = Convert.ToByte('C');
 for(int i=1;i<8;i++)
 {
 writefiledata[i] = writedata[i];
 }
 fs.Write(writefiledata,0,8);

 ManagedZLib.CompressionStream zlibStream = new ManagedZLib.CompressionStream(fs, ManagedZLib.CompressionOptions.CompressBest);

 BinaryWriter zlibWriter = new BinaryWriter(zlibStream);
 zlibWriter.Write(writedata,8,writedata.Length - 8);
    
   
 zlibWriter.Close();
 zlibStream.Close();
   
    
 r.Close();
 fs1.Close();
   
  
}

解壓縮:

程式碼FileStream stream [color=#0000ff]= new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader reader = new BinaryReader(stream);
try
{
 byte[] dataPart = new byte[stream.Length-8];
 string mark = new string(reader.ReadChars(3));

 if(mark == "FWS")
 {
  MessageBox.Show("已經是解壓縮過的檔案");
  return;
 }

 int version = Convert.ToInt32(reader.ReadByte());
 int fileLength = reader.ReadInt32();
 reader.Read(dataPart, 0, dataPart.Length);
 
 byte[] savedata = new byte[fileLength];

 savedata[0] = Convert.ToByte('F');
 savedata[1] = Convert.ToByte('W');
 savedata[2] = Convert.ToByte('S');
 savedata[3] = version;
 savedata[4] = Convert.ToByte(fileLength%256);
 savedata[5] = Convert.ToByte(fileLength/256%256);
 savedata[6] = Convert.ToByte(fileLength/256/256%256);
 savedata[7] = Convert.ToByte(fileLength/256/256/256%256);

 MemoryStream dataStream = new MemoryStream(dataPart);
 try
 {
 
  ManagedZLib.ManagedZLib.Initialize();
  ManagedZLib.CompressionStream zlibStream = new ManagedZLib.CompressionStream(dataStream, ManagedZLib.CompressionOptions.Decompress);
  BinaryReader zlibReader = new BinaryReader(zlibStream);
  
  try
  {
   byte[] decompressedPart = new byte[fileLength - 8];
   zlibReader.Read(decompressedPart, 0, decompressedPart.Length);
   for(int i=8;i<fileLength;i++)
   {
    savedata[i] = decompressedPart[i-8];
   }
   
   string FILE_NAME = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,filename);
   FileStream fss = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
   
   BinaryWriter ww = new BinaryWriter(fss);
   
   ww.Write(savedata);
   ww.Close();
   fss.Close();

  }
  finally
  {
   zlibReader.Close();
   zlibStream.Close();
   ManagedZLib.ManagedZLib.Terminate();
  }
  

 }
 finally
 {
  dataStream.Close();
 }
}
finally
{
 reader.Close();
 stream.Close();
}
[/color]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.