LoginSignup
6
2

More than 5 years have passed since last update.

OculusGoでZipを解凍する方法

Last updated at Posted at 2018-08-08

OculusGoはAndroidなので、Windowsとは違ったZIP解凍の手法が要求される。
ここでは方法を2つ紹介する。

環境: Unity 2018.1.5f1 (64-bit) Personal / OculusUtilities(1.26.0 2018/06/20)

事前設定

以下設定を行う。
この設定によって「端末内の写真やファイルへのアクセス許可」を得ることができる。

Edit>Project setting>PlayerSetting
    OtherSetting
        Configration
            Write Permission: External(SD Card)

方法1. UnityZipを使う

https://github.com/tsubaki/UnityZip
へアクセスし、Clone or DownloadからDonloadZIPを選択してダウンロード。

解凍して\Assets\Pluginsを使いたいプロジェクトへコピー
unityを起動して、エクスプローラーからAssetsペインにドラッグ&ドロップ

以下コードでwindowsでもiOSでもandroid(OculusGo)でも圧縮解凍できる。
とても簡単だが、大きいファイルを解凍すると終わるまで反応が帰ってこない。

string zipPath = Application.temporaryCachePath + "/dir/sample.zip";
string[] files = new string[]{ Application.temporaryCachePath + "/dir/target.txt" };
ZipUtil.Zip (zipPath, files);

string exportLocation = Application.temporaryCachePath + "/export";
ZipUtil.Unzip ( zipPath, exportLocation);

方法2. DotNetZip(Ionic.Zip.dll)を使う方法

概要

参考URL: https://qiita.com/satotin/items/39af44667c86db1e4069

方法1のUnityZipがWindows環境で解凍をするときに使っているDotNetZip(Ionic.Zip.dll)は、実はunityプロジェクトだとAndroid(OculusGo)上でも動く。
おそらく.netの世界で解凍処理を全部やっているためだと思われる。

DotNetZip は多くの機能を持っており、解凍せずにファイル一覧を得たり、解凍の進捗を表示したりすることができるが詳しい使い方はここでは説明しない。(DotNetZipの使い方自体はググれば出てくる)

利用方法

Ionic.Zip.dllは上のUnityZipに含まれているので、それを/pluginsに入れておく。

それだけだと利用するときに「Encoding name 'IBM437' not supported. Parameter name:name」というシステムエラーが出て落ちてしまう。
(unityインストールフォルダ)\Editor\Data\Mono\lib\mono\2.0\I18N.West.dll
……をコピーして、それも/pluginsに入れておくことで解決する。

ファイル名一覧を取得し、解凍する

using(Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(file)) {
    foreach (Ionic.Zip.ZipEntry entry in zip) {
        Debug.Log(entry.FileName);
        entry.Extract(exportPath);
    }
}

解凍せずに中身を読む

using(Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(file)) {
    Ionic.Zip.ZipEntry entry = zip[0];
    using (var st = entry.OpenReader()) {
        var reader = new System.IO.StreamReader(st);
        // ...
    }
}

zip書庫内ファイル名に日本語があっても文字化けしないようにする

参考URL: https://qiita.com/koukiwf/items/538aa8240187a249e316

上記コードでは省略していたが、windowsで圧縮したzipはShift_JISでエンコーディングされているため、エンコーディングを指定しないと日本語が化ける。

(unityインストールフォルダ)\Editor\Data\Mono\lib\mono\2.0\I18N.dll
(unityインストールフォルダ)\Editor\Data\Mono\lib\mono\2.0\I18N.CJK.dll
……をコピーして、/pluginsに入れた上で以下のように書くことで、zip書庫内の日本語ファイルを正常に読める。

var opt = new Ionic.Zip.ReadOptions();
opt.Encoding = System.Text.Encoding.GetEncoding("shift_jis");
using(Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(file, opt)) {
    // ...
}
6
2
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
6
2