mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.9.2.1
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Bry_ary {
|
||||
private byte[][] ary; private int len, max;
|
||||
public Bry_ary(int max) {
|
||||
this.len = 0;
|
||||
this.max = max;
|
||||
this.ary = new byte[max][];
|
||||
}
|
||||
public byte[][] Ary() {return ary;}
|
||||
public void Clear() {
|
||||
for (int i = 0; i < len; ++i)
|
||||
ary[i] = null;
|
||||
len = 0;
|
||||
}
|
||||
public int Len() {return len;}
|
||||
public void Add(byte[] v) {
|
||||
if (len == max) {
|
||||
int new_max = max * 2;
|
||||
byte[][] new_ary = new byte[new_max][];
|
||||
for (int i = 0; i < len; ++i)
|
||||
new_ary[i] = ary[i];
|
||||
this.ary = new_ary;
|
||||
this.max = new_max;
|
||||
}
|
||||
ary[len] = v;
|
||||
++len;
|
||||
}
|
||||
public byte[] Get_at(int i) {return ary[i];}
|
||||
public byte[] Get_at_last() {return len == 0 ? null : ary[len - 1];}
|
||||
public void Set_at_last(byte[] v) {ary[len - 1] = v;}
|
||||
public void Set_at(int i, byte[] v) {ary[i] = v;}
|
||||
public byte[][] To_ary(int rel) {
|
||||
if (len == 0) return Bry_.Ary_empty;
|
||||
int rv_len = len + rel;
|
||||
byte[][] rv = new byte[rv_len][];
|
||||
for (int i = 0; i < rv_len; ++i)
|
||||
rv[i] = ary[i];
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import gplx.core.primitives.*;
|
||||
public class Bry_cache {
|
||||
public byte[] Get_or_new(String v) {return Get_or_new(Bry_.new_u8(v));}
|
||||
public byte[] Get_or_new(byte[] v) {
|
||||
if (v.length == 0) return Bry_.Empty;
|
||||
Object rv = hash.Get_by(hash_ref.Val_(v));
|
||||
if (rv == null) {
|
||||
Bry_obj_ref bry = Bry_obj_ref.new_(v);
|
||||
hash.Add_as_key_and_val(bry);
|
||||
return v;
|
||||
}
|
||||
else
|
||||
return ((Bry_obj_ref)rv).Val();
|
||||
}
|
||||
Hash_adp hash = Hash_adp_.new_(); Bry_obj_ref hash_ref = Bry_obj_ref.null_();
|
||||
public static final Bry_cache _ = new Bry_cache(); Bry_cache() {}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_2_ref {
|
||||
public Int_2_ref() {}
|
||||
public Int_2_ref(int v0, int v1) {Val_all_(v0, v1);}
|
||||
public int Val_0() {return val_0;} public Int_2_ref Val_0_(int v) {val_0 = v; return this;} private int val_0;
|
||||
public int Val_1() {return val_1;} public Int_2_ref Val_1_(int v) {val_1 = v; return this;} private int val_1;
|
||||
public Int_2_ref Val_all_(int v0, int v1) {val_0 = v0; val_1 = v1; return this;}
|
||||
@Override public int hashCode() {
|
||||
int hash = 23;
|
||||
hash = (hash * 31) + val_0;
|
||||
hash = (hash * 31) + val_1;
|
||||
return hash;
|
||||
}
|
||||
@Override public boolean equals(Object obj) {
|
||||
if (obj == null) return false;
|
||||
Int_2_ref comp = (Int_2_ref)obj;
|
||||
return val_0 == comp.val_0 && val_1 == comp.val_1;
|
||||
}
|
||||
public static Int_2_ref parse(String raw) {
|
||||
try {
|
||||
String[] itms = String_.Split(raw, ",");
|
||||
int v0 = Int_.parse(itms[0]);
|
||||
int v1 = Int_.parse(itms[1]);
|
||||
return new Int_2_ref(v0, v1);
|
||||
} catch (Exception e) {Err_.Noop(e); throw Err_.new_parse("Int_2_ref", raw);}
|
||||
}
|
||||
public static Int_2_ref[] parse_ary_(String raw) {
|
||||
try {
|
||||
String[] itms = String_.Split(raw, ";");
|
||||
int itms_len = itms.length;
|
||||
Int_2_ref[] rv = new Int_2_ref[itms_len];
|
||||
for (int i = 0; i < itms_len; i++) {
|
||||
String[] vals = String_.Split(itms[i], ",");
|
||||
int v0 = Int_.parse(vals[0]);
|
||||
int v1 = Int_.parse(vals[1]);
|
||||
rv[i] = new Int_2_ref(v0, v1);
|
||||
}
|
||||
return rv;
|
||||
} catch (Exception e) {Err_.Noop(e); throw Err_.new_parse("Int_2_ref[]", raw);}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_2_val {
|
||||
public Int_2_val(int v0, int v1) {val_0 = v0; val_1 = v1;}
|
||||
public int Val_0() {return val_0;} final int val_0;
|
||||
public int Val_1() {return val_1;} final int val_1;
|
||||
public String Xto_str(Bry_bfr bfr) {return Xto_str(bfr, val_0, val_1);}
|
||||
public static final Int_2_val Null_ptr = null;
|
||||
public static Int_2_val parse(String raw) {
|
||||
String[] itms = String_.Split(raw, ',');
|
||||
if (itms.length != 2) return Null_ptr;
|
||||
int v0 = Int_.parse_or(itms[0], Int_.Min_value); if (v0 == Int_.Min_value) return Null_ptr;
|
||||
int v1 = Int_.parse_or(itms[1], Int_.Min_value); if (v1 == Int_.Min_value) return Null_ptr;
|
||||
return new Int_2_val(v0, v1);
|
||||
}
|
||||
public static String Xto_str(Bry_bfr bfr, int x, int y) {return bfr.Add_int_variable(x).Add_byte_comma().Add_int_variable(y).Xto_str_and_clear();}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_ary {
|
||||
private int[] ary; private int len, max;
|
||||
public Int_ary(int max) {
|
||||
this.len = 0;
|
||||
this.max = max;
|
||||
this.ary = new int[max];
|
||||
}
|
||||
public int[] Ary() {return ary;}
|
||||
public void Clear() {
|
||||
for (int i = 0; i < len; ++i)
|
||||
ary[i] = 0;
|
||||
len = 0;
|
||||
}
|
||||
public int Len() {return len;}
|
||||
public void Add(int v) {
|
||||
if (len == max) {
|
||||
int new_max = max * 2;
|
||||
int[] new_ary = new int[new_max];
|
||||
for (int i = 0; i < len; ++i)
|
||||
new_ary[i] = ary[i];
|
||||
this.ary = new_ary;
|
||||
this.max = new_max;
|
||||
}
|
||||
ary[len] = v;
|
||||
++len;
|
||||
}
|
||||
public int Pop_or(int or) {
|
||||
if (len == 0) return or;
|
||||
int rv = ary[len - 1];
|
||||
--len;
|
||||
return rv;
|
||||
}
|
||||
public int Idx_of(int key) {
|
||||
for (int i = len - 1; i > -1; --i) {
|
||||
int val = ary[i];
|
||||
if (val == key) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public boolean Del_by_key_from_end(int key) {
|
||||
int key_idx = Idx_of(key); if (key_idx == -1) return false;
|
||||
int last_idx = len - 1;
|
||||
for (int i = key_idx; i < last_idx; ++i)
|
||||
ary[i] = ary[i + 1];
|
||||
ary[last_idx] = 0;
|
||||
--len;
|
||||
return true;
|
||||
}
|
||||
public int Get_at(int i) {return ary[i];}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_ary_parser extends Obj_ary_parser_base {
|
||||
Number_parser parser = new Number_parser(); int[] ary; int ary_idx;
|
||||
public int[] Parse_ary(String str, byte dlm) {byte[] bry = Bry_.new_u8(str); return Parse_ary(bry, 0, bry.length, dlm);}
|
||||
public int[] Parse_ary(byte[] bry, int bgn, int end, byte dlm) {
|
||||
Parse_core(bry, bgn, end, dlm, Byte_ascii.Null);
|
||||
return ary;
|
||||
}
|
||||
@Override protected void Ary_len_(int v) {
|
||||
if (v == 0)
|
||||
ary = Int_.Ary_empty;
|
||||
else {
|
||||
ary = new int[v]; // NOTE: always create new array; never reuse;
|
||||
ary_idx = 0;
|
||||
}
|
||||
}
|
||||
@Override protected void Parse_itm(byte[] bry, int bgn, int end) {
|
||||
parser.Parse(bry, bgn, end); if (parser.Has_err() || parser.Has_frac()) throw Err_.new_wo_type("failed to parse number", "val", String_.new_u8(bry, bgn, end));
|
||||
ary[ary_idx++] = parser.Rv_as_int();
|
||||
}
|
||||
public static final Int_ary_parser _ = new Int_ary_parser();
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class Int_ary_parser_tst {
|
||||
@Test public void Many() {tst_ints("1,2,3,4,5" , 0, 9, Int_.Ary(1, 2, 3, 4, 5));}
|
||||
@Test public void One() {tst_ints("1" , 0, 1, Int_.Ary(1));}
|
||||
@Test public void None() {tst_ints("" , 0, 0, Int_.Ary());}
|
||||
private void tst_ints(String raw, int bgn, int end, int[] expd) {
|
||||
int[] actl = Int_ary_parser._.Parse_ary(Bry_.new_a7(raw), bgn, end, Byte_ascii.Comma);
|
||||
Tfds.Eq_ary(expd, actl);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Int_list {
|
||||
private int[] ary = Int_.Ary_empty; private int ary_len, ary_max;
|
||||
public void Add(int uid) {
|
||||
int new_len = ary_len + 1;
|
||||
if (new_len > ary_max) {
|
||||
ary_max += 16;
|
||||
int[] new_ary = new int[ary_max];
|
||||
Int_.Ary_copy_to(ary, ary_len, new_ary);
|
||||
ary = new_ary;
|
||||
}
|
||||
ary[ary_len] = uid;
|
||||
ary_len = new_len;
|
||||
}
|
||||
public int Len() {return ary_len;}
|
||||
public int Get_at(int i) {return ary[i];}
|
||||
public void Clear() {
|
||||
ary = Int_.Ary_empty;
|
||||
ary_len = ary_max = 0;
|
||||
}
|
||||
public static Int_list new_(int... ary) {
|
||||
Int_list rv = new Int_list();
|
||||
int len = ary.length;
|
||||
rv.ary = ary; rv.ary_len = len; rv.ary_max = len;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public interface Io_zip_mgr {
|
||||
void Zip_fil(Io_url src_fil, Io_url trg_fil);
|
||||
byte[] Zip_bry(byte[] src, int bgn, int len);
|
||||
byte[] Unzip_bry(byte[] src, int bgn, int len);
|
||||
void Unzip_to_dir(Io_url src_fil, Io_url trg_dir);
|
||||
void Zip_dir(Io_url src_dir, Io_url trg_fil);
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import java.io.*;
|
||||
import java.util.zip.*;
|
||||
public class Io_zip_mgr_base implements Io_zip_mgr {
|
||||
public void Zip_fil(Io_url src_fil, Io_url trg_fil) {
|
||||
byte[] src_bry = Io_mgr.I.LoadFilBry(src_fil);
|
||||
byte[] trg_bry = Zip_bry(src_bry, 0, src_bry.length);
|
||||
Io_mgr.I.SaveFilBry(trg_fil, trg_bry);
|
||||
}
|
||||
public void Zip_dir(Io_url src_dir, Io_url trg_fil) {
|
||||
try {
|
||||
byte[] bry = new byte[4096];
|
||||
FileOutputStream fil_strm = new FileOutputStream(trg_fil.Raw());
|
||||
ZipOutputStream zip_strm = new ZipOutputStream(fil_strm);
|
||||
Zip_dir__add_dir(zip_strm, bry, "", src_dir, Zip_dir__get_subs(src_dir));
|
||||
zip_strm.flush();
|
||||
zip_strm.close();
|
||||
} catch(IOException e) {Err_.new_exc(e, "io", "error duing zip", "src", src_dir.Raw(), "trg", trg_fil.Raw());}
|
||||
}
|
||||
private void Zip_dir__add_dir(ZipOutputStream zip_strm, byte[] bry, String zip_path, Io_url owner_dir, Io_url[] subs) {
|
||||
int len = subs.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Io_url sub = subs[i];
|
||||
String sub_path = zip_path + sub.NameAndExt_noDirSpr();
|
||||
if (sub.Type_dir())
|
||||
Zip_dir__add_dir(zip_strm, bry, sub_path + "/", sub, Zip_dir__get_subs(sub));
|
||||
else
|
||||
Zip_dir__add_fil(zip_strm, bry, sub_path, sub);
|
||||
}
|
||||
}
|
||||
private void Zip_dir__add_fil(ZipOutputStream zip_strm, byte[] bry, String zip_path, Io_url fil_url) {
|
||||
try {
|
||||
int len;
|
||||
FileInputStream fil_strm = new FileInputStream(fil_url.Raw());
|
||||
zip_strm.putNextEntry(new ZipEntry(zip_path));
|
||||
while ((len = fil_strm.read(bry)) > 0)
|
||||
zip_strm.write(bry, 0, len);
|
||||
fil_strm.close();
|
||||
} catch(IOException e) {throw Err_.new_exc(e, "io", "error duing zip", "src", zip_path);}
|
||||
}
|
||||
private Io_url[] Zip_dir__get_subs(Io_url url) {
|
||||
return Io_mgr.I.QueryDir_args(url).DirInclude_().ExecAsUrlAry();
|
||||
}
|
||||
public byte[] Zip_bry(byte[] src, int bgn, int len) {
|
||||
ByteArrayInputStream src_stream = new ByteArrayInputStream(src, bgn, len);
|
||||
ByteArrayOutputStream trg_stream = new ByteArrayOutputStream(len);
|
||||
try {
|
||||
ZipOutputStream trgZip = new ZipOutputStream(trg_stream);
|
||||
ZipEntry entry = new ZipEntry("file");
|
||||
trgZip.putNextEntry(entry);
|
||||
int count;
|
||||
while((count = src_stream.read(tmp, 0, tmpLen)) != -1) {
|
||||
trgZip.write(tmp, 0, count);
|
||||
}
|
||||
trgZip.close();
|
||||
} catch(Exception e) {throw Err_.new_wo_type("failed to zip", "err", e.getMessage());}
|
||||
return trg_stream.toByteArray();
|
||||
}
|
||||
public byte[] Unzip_bry(byte[] src, int bgn, int len) {
|
||||
ByteArrayInputStream src_stream = new ByteArrayInputStream(src, bgn, len);
|
||||
ByteArrayOutputStream trg_stream = new ByteArrayOutputStream(len);
|
||||
try {
|
||||
ZipInputStream srcZip = new ZipInputStream(src_stream);
|
||||
int count;
|
||||
while(srcZip.getNextEntry() != null) {
|
||||
while ((count = srcZip.read(tmp, 0, tmpLen)) != -1) {
|
||||
trg_stream.write(tmp, 0, count);
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {throw Err_.new_wo_type("failed to unzip", "err", e.getMessage());}
|
||||
return trg_stream.toByteArray();
|
||||
}
|
||||
public void Unzip_to_dir(Io_url src_fil, Io_url trg_dir) {
|
||||
byte[] buffer = new byte[4096];
|
||||
try{
|
||||
Io_mgr.I.CreateDirIfAbsent(trg_dir);
|
||||
|
||||
ZipInputStream zip_strm = new ZipInputStream(new FileInputStream(src_fil.Raw()));
|
||||
ZipEntry zip_eny = zip_strm.getNextEntry();
|
||||
while (zip_eny != null) {
|
||||
String itm_name = zip_eny.getName();
|
||||
if (Op_sys.Cur().Tid_is_wnt()) itm_name = String_.Replace(itm_name, "/", "\\");
|
||||
Io_url itm_url = Io_url_.new_any_(trg_dir.GenSubFil(itm_name).Raw());
|
||||
Io_mgr.I.CreateDirIfAbsent(itm_url.OwnerDir()); // make sure owner dir exists
|
||||
if (itm_url.Type_fil()) {
|
||||
Io_mgr.I.SaveFilStr_args(itm_url, "").Exec();
|
||||
File itm_file = new File(itm_url.Raw());
|
||||
FileOutputStream itm_strm = new FileOutputStream(itm_file);
|
||||
int len;
|
||||
while ((len = zip_strm.read(buffer)) > 0)
|
||||
itm_strm.write(buffer, 0, len);
|
||||
itm_strm.close();
|
||||
}
|
||||
zip_eny = zip_strm.getNextEntry();
|
||||
}
|
||||
zip_strm.closeEntry();
|
||||
zip_strm.close();
|
||||
} catch(IOException e) {throw Err_.new_exc(e, "io", "error duing unzip", "src", src_fil.Raw(), "trg", trg_dir.Raw());}
|
||||
}
|
||||
byte[] tmp = new byte[4096]; int tmpLen = 4096;
|
||||
public static final Io_zip_mgr _ = new Io_zip_mgr_base();
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
public class Io_zip_mgr_mok implements Io_zip_mgr {
|
||||
public void Zip_fil(Io_url src_fil, Io_url trg_fil) {
|
||||
byte[] src_bry = Io_mgr.I.LoadFilBry(src_fil);
|
||||
byte[] zip_bry = Zip_bry(src_bry, 0, src_bry.length);
|
||||
Io_mgr.I.SaveFilBry(trg_fil, zip_bry);
|
||||
}
|
||||
public void Zip_dir(Io_url src_dir, Io_url trg_fil) {}
|
||||
public byte[] Zip_bry(byte[] src, int bgn, int len) {return Bry_.Add(Bry_zipped, Bry_.Mid(src, bgn, len));}
|
||||
public byte[] Unzip_bry(byte[] src, int bgn, int len) {
|
||||
if (src == Bry_.Empty) return src;
|
||||
byte[] section = Bry_.Mid(src, bgn, bgn + len);
|
||||
if (!Bry_.Has_at_bgn(section, Bry_zipped, 0, section.length)) throw Err_.new_wo_type("src not zipped", "section", String_.new_u8(section));
|
||||
return Bry_.Mid(section, Bry_zipped.length, section.length);
|
||||
}
|
||||
public void Unzip_to_dir(Io_url src_fil, Io_url trg_dir) {}
|
||||
private static final byte[] Bry_zipped = Bry_.new_a7("zipped:");
|
||||
public static final Io_zip_mgr_mok _ = new Io_zip_mgr_mok(); Io_zip_mgr_mok() {}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx;
|
||||
import org.junit.*;
|
||||
public class Io_zip_mgr_tst {
|
||||
@Test public void Zip_unzip() {
|
||||
Zip_unzip_tst("abcdefghijklmnopqrstuvwxyz");
|
||||
}
|
||||
private void Zip_unzip_tst(String s) {
|
||||
Io_zip_mgr zip_mgr = Io_zip_mgr_base._;
|
||||
byte[] src = Bry_.new_a7(s);
|
||||
byte[] zip = zip_mgr.Zip_bry(src, 0, src.length);
|
||||
byte[] unz = zip_mgr.Unzip_bry(zip, 0, zip.length);
|
||||
Tfds.Eq_ary(src, unz);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user