LoginSignup
0
0

More than 5 years have passed since last update.

PCLに埋め込んだCocosSharpコードからZIPを取り出そうとしたら実機で動かない件

Posted at

ものすごいピンポイントですが、困ったことと試したこと、最終的にうまく行った方法が誰かの役に立てば…。

■環境

  • Windows 10
  • Visual Studio Enterprise 2015 V14.0.25431.01 Update 3
  • Xamarin 4.2.0.703

■前提

  • VS+Xamarinで、iOS/Androidマルチなアプリを作成
  • PCLを介してコードを共通化
  • 一部、CocosSharpを使ったゲーム部分あり

■事象

 PCL側に埋め込んだZIPファイルが、Releaseビルドおよびapkインストールで読み込みエラーになる。

問題のコード

sample1
    using (var zip = new ZipArchive(stream))
    using (TextReader reader = new StreamReader(zip.GetEntry(key + ".txt").Open()))
    {
        var strList = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        return strList.ToList();
    }

発生するエラー

    Xamarin caused by: android.runtime.JavaProxyThrowable: System.TypeInitializationException: The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception. ---> System.NotSupportedException: Encoding 932 data could not be found. Make sure you have correct international codeset assembly installed and enabled.
    System.Text.Encoding.GetEncoding(int codepage)<368820a9888f43ddb85d18e87189adbf>:0
    at SharpCompress.Common.ArchiveEncoding..cctor () [0x0000f] in <bdb2162ce57c47048969b76e22afb75f>:0
    --- End of inner exception stack trace ---
    Hoge.Fuga.Piyo.ExtractZip(string key)<e2bccd13542e46cdaa968185aed83fc8>:0

■解法

 Android側のプロジェクトのプロパティから以下の通り設定
 Android Options>Linker>Additional supported encodings>CJK を追加

■根本原因(予測)

 標準で含まれていないEncodingであるcp932を使おうとしてエラー

■他に試してダメだったこと

  • ZIPファイルをUTF8指定で圧縮(by.7z)

  Windowsではファイル名をsjisを使って圧縮するらしく、LinuxとWindowsでやり取りすると文字化けするような事象は多々あるようです。
  http://qiita.com/hoo89@github/items/46dcd8134061c392772f
  ちなみに今回は圧縮対象も、圧縮結果もアルファベットのみだったのでそもそも化けないんですが…

  • 読み込み場所でUTF8を使うように指定

  問題のコードをこんな感じに。。

sample2
    using (var zip = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.UTF8))
    using (TextReader reader = new StreamReader(zip.GetEntry(key + ".txt").Open(), Encoding.UTF8))
    {
        var strList = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        return strList.ToList();
    }
  • cp932を手動で追加できるようにコーディング

  http://aquasoftware.net/blog/?p=895
  http://vnext-y-blog.azurewebsites.net/archives/1191
  そもそもPCLはProfile7なのでSystem.Text.CodePagesEncodingProviderが入れられない。
  DLLをコピーするのも何が起こるかわからんので避けたい。。

■最後に

 根本原因が本当に合っているのか、ほかに手法がないのか、まだまだ調べないといけないことはある気がします。
 …が、取り急ぎ動くこと最優先するのでお茶を濁す形でFinishです。

コメント、アドバイス、これってどうなん?
歓迎です!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0