【问题标题】:Programmatically Edit a .webarchive File以编程方式编辑 .webarchive 文件
【发布时间】:2011-10-27 02:21:33
【问题描述】:

我正在使用 Actionscript 构建一个 AIR 应用程序,并且我想以编程方式将一段文本插入到 .webarchive 文件中。问题是每次我插入文本时,文件都会以某种方式损坏。我使用的代码如下所示:

var stream:FileStream = new FileStream();                       
stream.open(file, FileMode.READ);   
var body:ByteArray = new ByteArray();                       
stream.readBytes(body, file.size);                      
var result:Array = pattern.exec(body.toString());                   
var new_body:String;                        
new_body = body.toString().replace(pattern, "replacing text here!</body>"); 
stream.close();                     
stream.open(file, FileMode.WRITE);                      
stream.writeUTFBytes(new_body);                     
stream.close();

我猜这个问题与 .webarchive 文件的编码有关。有没有人对如何解决这个问题有任何想法?提前致谢!

【问题讨论】:

    标签: actionscript-3 apache-flex safari flash-builder webarchive


    【解决方案1】:

    从文件中读取文本信息时,您应该始终使用stream.readUTFBytes()stream.readUTF()。我猜当您在代码中将字节转换为字符串时会出现一些实际的编码问题。正确的代码是:

    var stream:FileStream = new FileStream();                       
    stream.open(file, FileMode.READ);   
    var body:String = stream.readUTFBytes(stream.bytesAvailable);   
    stream.close();               
    var new_body:String = body.replace(pattern, "replacing text here!</body>");  
    stream.open(file, FileMode.WRITE);                      
    stream.writeUTFBytes(new_body);                     
    stream.close();
    

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多