package com.smtscript.lib; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.attribute.FileTime; import java.util.Base64; import java.util.Enumeration; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.mozilla.javascript.Context; import org.mozilla.javascript.NativeFunction; import org.mozilla.javascript.NativeObject; import com.smtscript.lib.zip.JSZipReaderEntry; import com.smtscript.lib.zip.JSZipWriter; import com.smtscript.run.ScriptRunScope.FileInfo; import com.smtscript.utils.SMTStatic; public class JSStaticZip extends JSStaticAbstract { public void setEntryCreateTime(JSZipReaderEntry entry, String stime) { FileTime time = FileTime.fromMillis(SMTStatic.toDate(stime).getTime()); entry._entry.setCreationTime(time); } public void setEntryModifyTime(JSZipReaderEntry entry, String stime) { FileTime time = FileTime.fromMillis(SMTStatic.toDate(stime).getTime()); entry._entry.setLastModifiedTime(time); } public void setEntryAccessTime(JSZipReaderEntry entry, String stime) { FileTime time = FileTime.fromMillis(SMTStatic.toDate(stime).getTime()); entry._entry.setLastAccessTime(time); } public String getEntryName(JSZipReaderEntry entry) { return entry._entry.getName(); } public long getEntrySize(JSZipReaderEntry entry) { return entry._entry.getSize(); } public boolean isDirectoryEntry(JSZipReaderEntry entry) { return entry._entry.isDirectory(); } public String readTextEntry(JSZipReaderEntry entry) throws IOException { return readTextEntry(entry, "UTF-8"); } public void saveEntryToFile(JSZipReaderEntry entry, String fileName) throws Exception { InputStream is = entry._zip.getInputStream(entry._entry); FileOutputStream os = new FileOutputStream(fileName); try { int ch; while((ch = is.read()) >= 0) { os.write(ch); } } finally { os.close(); } } public String readTextEntry(JSZipReaderEntry entry, String encode) throws IOException { return SMTStatic.readTextStream(entry._zip.getInputStream(entry._entry), encode); } @JSComment( "config - JSON\n" + " file - Srting : zip file name\n" + " read - function(entry)\n" ) public void readZip(NativeObject config) throws Exception { String fileName = (String) SMTStatic.getJSValue(config, "file"); NativeFunction readFunc = (NativeFunction) SMTStatic.getJSValue(config, "read"); FileInfo file = _parentScope.__findExistFile__(fileName, true); Context cx = _parentScope.__runtime__().entryContext(); ZipFile zip = new ZipFile(file.getName()); try { Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); readFunc.call(cx, readFunc, null, new Object[]{new JSZipReaderEntry(zip, entry)}); } } finally { zip.close(); Context.exit(); } } public JSZipWriter createZip(String fileName) throws Exception { return new JSZipWriter(this, fileName); } public String compressText(String text) throws Exception { return compressBytes(text.getBytes("UTF-8")); } public String compressBase64(String base64) throws Exception { return compressBytes(Base64.getDecoder().decode(base64)); } public String uncompressText(String base64) throws Exception { byte[] data = uncompress(Base64.getDecoder().decode(base64)); return new String(data, "UTF-8"); } public String uncompressBase64(String base64) throws Exception { byte[] data = uncompress(Base64.getDecoder().decode(base64)); return Base64.getEncoder().encodeToString(data); } private String compressBytes(byte[] data) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip; gzip = new GZIPOutputStream(out); gzip.write(data); gzip.close(); return Base64.getEncoder().encodeToString(out.toByteArray()); } private byte[] uncompress(byte[] bytes) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); GZIPInputStream ungzip = new GZIPInputStream(in); byte[] buffer = new byte[4096]; int n; while ((n = ungzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } return out.toByteArray(); } }