Mercurial Hosting > luan
diff core/src/luan/modules/IoLuan.java @ 761:99356cfde2f0
remove horrible java zip
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 Jul 2016 01:52:20 -0600 |
parents | ae612dfc57cb |
children | d2f61d5be9cc |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Tue Jul 19 19:15:02 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Wed Jul 20 01:52:20 2016 -0600 @@ -32,9 +32,6 @@ import java.util.Enumeration; import java.util.List; import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -228,25 +225,6 @@ } } - public void unzip(Object path) throws IOException, LuanException { - File pathFile = objToFile(path); - if( pathFile==null ) - throw new LuanException( "bad argument #1 to 'unzip' (string or file table expected)" ); - ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream())); - ZipEntry entry; - while( (entry = in.getNextEntry()) != null ) { - if( entry.isDirectory() ) - continue; - File file = new File(pathFile,entry.getName()); - file.getParentFile().mkdirs(); - OutputStream out = new FileOutputStream(file); - Utils.copyAll(in,out); - out.close(); - file.setLastModified(entry.getTime()); - } - in.close(); - } - public LuanTable table() { LuanTable tbl = new LuanTable(); try { @@ -272,9 +250,6 @@ tbl.rawPut( "exists", new LuanJavaFunction( LuanIn.class.getMethod( "exists" ), this ) ); - tbl.rawPut( "unzip", new LuanJavaFunction( - LuanIn.class.getMethod( "unzip", Object.class ), this - ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -351,55 +326,6 @@ return binaryWriter(new BufferedOutputStream(outputStream())); } - public void zip(LuanState luan,Object basePathObj,LuanTable filePathList) throws LuanException, IOException { - File basePathFile = objToFile(basePathObj); - if( basePathFile==null ) - throw new LuanException( "bad argument #1 to 'zip' (string or file table expected)" ); - String[] filePaths; - if( filePathList==null ) { - File file = basePathFile.getCanonicalFile(); - filePaths = new String[]{file.toString()}; - basePathFile = file.getParentFile(); - } else { - List list = filePathList.asList(); - filePaths = new String[list.size()]; - for( int i=0; i<filePaths.length; i++ ) { - Object obj = list.get(i); - if( !(obj instanceof String) ) - throw new LuanException("file paths must be strings"); - filePaths[i] = (String)obj; - } - } - String basePath = basePathFile.toString() + '/'; - ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream())); - zip(out,basePath,filePaths); - out.close(); - } - - private static void zip(ZipOutputStream out,String basePath,String[] filePaths) throws LuanException, IOException { - for( String filePath : filePaths ) { - File file = new File(filePath); - if( file.isDirectory() ) { - String[] children = file.list(); - for( int i=0; i<children.length; i++ ) { - children[i] = filePath + "/" + children[i]; - } - zip(out,basePath,children); - } else { - if( !filePath.startsWith(basePath) ) - throw new LuanException(filePath+" not in "+basePath); - String relPath = filePath.substring(basePath.length()); - ZipEntry entry = new ZipEntry(relPath); - entry.setTime(file.lastModified()); - out.putNextEntry(entry); - InputStream in = new FileInputStream(file); - Utils.copyAll(in,out); - in.close(); - out.closeEntry(); - } - } - } - @Override public LuanTable table() { LuanTable tbl = super.table(); try { @@ -412,9 +338,6 @@ tbl.rawPut( "binary_writer", new LuanJavaFunction( LuanIO.class.getMethod( "binary_writer" ), this ) ); - tbl.rawPut( "zip", new LuanJavaFunction( - LuanIO.class.getMethod( "zip", LuanState.class, Object.class, LuanTable.class ), this - ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -725,7 +648,7 @@ add( schemes, "https", LuanState.class, String.class, LuanTable.class ); add( schemes, "luan", LuanState.class, String.class ); add( schemes, "stdin", LuanState.class ); - add( schemes, "os", String.class ); + add( schemes, "os", LuanState.class, String.class, LuanTable.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -818,10 +741,22 @@ public static final class LuanOs extends LuanIO { + private final String cmd; private final Process proc; - private LuanOs(String cmd) throws IOException { - this.proc = Runtime.getRuntime().exec(cmd); + private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { + this.cmd = cmd; + File dir = null; + if( options != null ) { + Map map = options.asMap(luan); + Object obj = map.remove("dir"); + dir = objToFile(obj); + if( dir==null ) + throw new LuanException( "bad option 'dir' (string or file table expected)" ); + if( !map.isEmpty() ) + throw new LuanException( "unrecognized options: "+map ); + } + this.proc = Runtime.getRuntime().exec(cmd,null,dir); } @Override public InputStream inputStream() throws IOException { @@ -855,7 +790,7 @@ int exitVal = proc.exitValue(); if( exitVal != 0 ) { Reader err = new InputStreamReader(proc.getErrorStream()); - String error = Utils.readAll(err); + String error = "error in: "+cmd+"\n"+Utils.readAll(err); err.close(); throw new LuanException(error); } @@ -880,8 +815,8 @@ } } - public static LuanTable os(String cmd) throws IOException { - return new LuanOs(cmd).table(); + public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException { + return new LuanOs(luan,cmd,options).table(); }