|
| 1 | +// This string is autogenerated by ChangeAppSettings.sh, do not change |
| 2 | +// spaces amount |
| 3 | +package org.renpy.android; |
| 4 | + |
| 5 | +import java.io.*; |
| 6 | + |
| 7 | +import android.app.Activity; |
| 8 | +import android.util.Log; |
| 9 | + |
| 10 | +import java.io.BufferedInputStream; |
| 11 | +import java.io.BufferedOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.io.FileInputStream; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.File; |
| 17 | + |
| 18 | +import java.util.zip.GZIPInputStream; |
| 19 | + |
| 20 | +import android.content.res.AssetManager; |
| 21 | + |
| 22 | +import org.kamranzafar.jtar.*; |
| 23 | + |
| 24 | +public class AssetExtract { |
| 25 | + |
| 26 | + private AssetManager mAssetManager = null; |
| 27 | + private Activity mActivity = null; |
| 28 | + |
| 29 | + public AssetExtract(Activity act) { |
| 30 | + mActivity = act; |
| 31 | + mAssetManager = act.getAssets(); |
| 32 | + } |
| 33 | + |
| 34 | + public boolean extractTar(String asset, String target) { |
| 35 | + |
| 36 | + byte buf[] = new byte[1024 * 1024]; |
| 37 | + |
| 38 | + InputStream assetStream = null; |
| 39 | + TarInputStream tis = null; |
| 40 | + |
| 41 | + try { |
| 42 | + assetStream = mAssetManager.open(asset, AssetManager.ACCESS_STREAMING); |
| 43 | + tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new BufferedInputStream(assetStream, 8192)), 8192)); |
| 44 | + } catch (IOException e) { |
| 45 | + Log.e("python", "opening up extract tar", e); |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + while (true) { |
| 50 | + TarEntry entry = null; |
| 51 | + |
| 52 | + try { |
| 53 | + entry = tis.getNextEntry(); |
| 54 | + } catch ( java.io.IOException e ) { |
| 55 | + Log.e("python", "extracting tar", e); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if ( entry == null ) { |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + Log.v("python", "extracting " + entry.getName()); |
| 64 | + |
| 65 | + if (entry.isDirectory()) { |
| 66 | + |
| 67 | + try { |
| 68 | + new File(target +"/" + entry.getName()).mkdirs(); |
| 69 | + } catch ( SecurityException e ) { }; |
| 70 | + |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + OutputStream out = null; |
| 75 | + String path = target + "/" + entry.getName(); |
| 76 | + |
| 77 | + try { |
| 78 | + out = new BufferedOutputStream(new FileOutputStream(path), 8192); |
| 79 | + } catch ( FileNotFoundException e ) { |
| 80 | + } catch ( SecurityException e ) { }; |
| 81 | + |
| 82 | + if ( out == null ) { |
| 83 | + Log.e("python", "could not open " + path); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + while (true) { |
| 89 | + int len = tis.read(buf); |
| 90 | + |
| 91 | + if (len == -1) { |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + out.write(buf, 0, len); |
| 96 | + } |
| 97 | + |
| 98 | + out.flush(); |
| 99 | + out.close(); |
| 100 | + } catch ( java.io.IOException e ) { |
| 101 | + Log.e("python", "extracting zip", e); |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + tis.close(); |
| 108 | + assetStream.close(); |
| 109 | + } catch (IOException e) { |
| 110 | + // pass |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | +} |
0 commit comments