AS3 代码中的文件(作为字节字符串)
我需要将文件作为字节字符串直接“嵌入”到 AS3 代码中,而不是将其作为外部资源调用。
所以这工作正常:
var testString = "537563636573733a20537472696e672072652d656e636f6465642e";
var testArray:ByteArray = new ByteArray();
var len:uint = testString.length;
trace("testString LENGTH: " + len.toString());
for (var i:uint = 0; i < len; i += 2) {
var c:String = '0x' + testString.charAt(i) + testString.charAt(i + 1);
if(i < 10) { trace("testString Byte: " + c); }
testArray.writeByte(parseInt(c));
}
trace("testString: " + testArray.toString());
trace("testString NUMBER OF BYTES: " + testArray.length.toString());
并在控制台中产生这个:
testString LENGTH: 54
testString Byte: 0x53
testString Byte: 0x75
testString Byte: 0x63
testString Byte: 0x63
testString Byte: 0x65
testString: Success: String re-encoded.
testString NUMBER OF BYTES: 27
接下来,我在十六进制编辑器(HxD)中打开目标文件,然后将字节直接复制并粘贴到我的字符串变量中,就像上面一样,我在控制台中得到以下输出:
testString LENGTH: 97478
testString Byte: 0x50
testString Byte: 0x4B
testString Byte: 0x03
testString Byte: 0x04
testString Byte: 0x14
testString: PK```
testString NUMBER OF BYTES: 48739
...并且该文件(作为 ByteArray)无法被同一个库读取,该库在将其用作外部资源(使用 URLLoader)时完美地读取它。
我确实尝试将字节字符串从我的代码中复制回来,然后粘贴它进入十六进制编辑器并将其保存为文件,并且该文件已正确重新创建,因此我认为这不是复制和粘贴问题。此外,从每个字节字符串的前面删除“0x”并使用“parseInt(c, 16)”进行解析会产生完全相同的结果。
对于一些额外的背景,目标文件是 KMZ 3D 模型,并且该文件正在由 Papervision3D 的 KMZ.as 库进行解析,该库使用 Nochump 库来解压缩 KMZ 文件。当我尝试将 ByteArray 传递给 KMZ.as 时收到的错误消息是:
Error: invalid zip
at nochump.util.zip::ZipFile/findEND()
at nochump.util.zip::ZipFile/readEND()
at nochump.util.zip::ZipFile/readEntries()
at nochump.util.zip::ZipFile()
at org.papervision3d.objects.parsers::KMZ/parse()
at org.papervision3d.objects.parsers::KMZ/load()
at infoModel/initKMZ2()
at infoModel()
任何想法和/或建议将不胜感激。
饲料
I need to 'embed' a file as a string of bytes directly in AS3 code rather than calling it as an external resource.
So this works fine:
var testString = "537563636573733a20537472696e672072652d656e636f6465642e";
var testArray:ByteArray = new ByteArray();
var len:uint = testString.length;
trace("testString LENGTH: " + len.toString());
for (var i:uint = 0; i < len; i += 2) {
var c:String = '0x' + testString.charAt(i) + testString.charAt(i + 1);
if(i < 10) { trace("testString Byte: " + c); }
testArray.writeByte(parseInt(c));
}
trace("testString: " + testArray.toString());
trace("testString NUMBER OF BYTES: " + testArray.length.toString());
And produces this in the console:
testString LENGTH: 54
testString Byte: 0x53
testString Byte: 0x75
testString Byte: 0x63
testString Byte: 0x63
testString Byte: 0x65
testString: Success: String re-encoded.
testString NUMBER OF BYTES: 27
So next I open my target file in a Hex editor (HxD) and copy and paste the bytes directly into my String variable, just like above and I get the following output to the console:
testString LENGTH: 97478
testString Byte: 0x50
testString Byte: 0x4B
testString Byte: 0x03
testString Byte: 0x04
testString Byte: 0x14
testString: PK```
testString NUMBER OF BYTES: 48739
...and the file (as a ByteArray) is unreadable by the same library that read it perfectly when it used it as an external resource (using URLLoader.)
I did try copying the byte string back out of my code, pasting it into the Hex editor and saving it as a file and the file was re-created correctly so I don't think it is a copy and paste issue. Also, removing the '0x' from the front of each byte string and parsing with "parseInt(c, 16)" produces exactly the same results.
For some additional background, the target file is a KMZ 3D model and the file is being parsed by Papervision3D's KMZ.as library which uses the Nochump library to unzip the KMZ file. The error message I get back when trying to pass the ByteArray to KMZ.as is:
Error: invalid zip
at nochump.util.zip::ZipFile/findEND()
at nochump.util.zip::ZipFile/readEND()
at nochump.util.zip::ZipFile/readEntries()
at nochump.util.zip::ZipFile()
at org.papervision3d.objects.parsers::KMZ/parse()
at org.papervision3d.objects.parsers::KMZ/load()
at infoModel/initKMZ2()
at infoModel()
Any thoughts and/or suggestions would be greatly appreciated.
fodder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不将数据存储为字节数组而不是字符串。这将更加紧凑(每个字节一个字节,而不是字符串所需的多个),并且您不必解析回 int。
Why not just store the data as an array of bytes instead of a string. That'll be a lot more compact (one byte per byte, instead of the multiple needed by the string), and you wouldn't have to parse back to an int.
使用
[Embed()]
标记。现在,myKMZ 将是一个扩展 ByteArray 的类。
数据取出方法:
Use the
[Embed()]
tag.Now, myKMZ will be a class that extends ByteArray.
How to get the data out: