Mercurial Hosting > luan
diff core/src/luan/modules/IoLuan.java @ 746:293c397e8dee
improve zip
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Jul 2016 19:36:02 -0600 |
parents | 9c1f28b26395 |
children | d3a1e9a48a94 |
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java Wed Jul 13 21:27:23 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Thu Jul 14 19:36:02 2016 -0600 @@ -312,6 +312,41 @@ return binaryWriter(new BufferedOutputStream(outputStream())); } + public void zip(LuanState luan,String basePath,String... filePaths) throws LuanException, IOException { + if( filePaths.length == 0 ) { + File file = new File(basePath).getCanonicalFile(); + filePaths = new String[]{file.toString()}; + basePath = file.getParent(); + } + if( !basePath.endsWith("/") ) + basePath += '/'; + 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()); + out.putNextEntry(new ZipEntry(relPath)); + InputStream in = new FileInputStream(file); + Utils.copyAll(in,out); + in.close(); + out.closeEntry(); + } + } + } + @Override public LuanTable table() { LuanTable tbl = super.table(); try { @@ -324,6 +359,9 @@ tbl.rawPut( "binary_writer", new LuanJavaFunction( LuanIO.class.getMethod( "binary_writer" ), this ) ); + tbl.rawPut( "zip", new LuanJavaFunction( + LuanIO.class.getMethod( "zip", LuanState.class, String.class, new String[0].getClass() ), this + ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -779,7 +817,7 @@ return tbl; } */ - +/* // files maps zip name to uri public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException { Object obj = uri(luan,zipUri,null).rawGet("java"); @@ -808,7 +846,7 @@ } out.close(); } - +*/ // security