Mercurial Hosting > luan
changeset 706:30c87c859277
add Io.zip
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 17 May 2016 19:00:25 -0600 |
parents | 52ecb629a634 |
children | 1ed9e55f0be8 |
files | core/src/luan/modules/Io.luan core/src/luan/modules/IoLuan.java |
diffstat | 2 files changed, 34 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/modules/Io.luan Tue May 17 16:42:47 2016 -0600 +++ b/core/src/luan/modules/Io.luan Tue May 17 19:00:25 2016 -0600 @@ -8,6 +8,7 @@ M.read_console_line = IoLuan.read_console_line M.schemes = IoLuan.newSchemes() M.uri = IoLuan.uri +M.zip = IoLuan.zip M.stdin = IoLuan.defaultStdin.table() M.socket_server = IoLuan.socket_server M.stdout = IoLuan.textWriter(System.out)
--- a/core/src/luan/modules/IoLuan.java Tue May 17 16:42:47 2016 -0600 +++ b/core/src/luan/modules/IoLuan.java Tue May 17 19:00:25 2016 -0600 @@ -29,6 +29,9 @@ import java.net.UnknownHostException; import java.util.List; import java.util.ArrayList; +import java.util.Map; +import java.util.zip.ZipOutputStream; +import java.util.zip.ZipEntry; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -712,6 +715,36 @@ } + // files maps zip name to uri + public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException { + Object obj = uri(luan,zipUri).rawGet("java"); + if( !(obj instanceof LuanIO) ) + throw new LuanException("invalid uri for zip"); + LuanIO zipIo = (LuanIO)obj; + ZipOutputStream out = new ZipOutputStream(zipIo.outputStream()); + for( Map.Entry<Object,Object> entry : files.iterable(luan) ) { + obj = entry.getKey(); + if( !(obj instanceof String) ) + throw new LuanException("zip file table keys must be strings"); + String fileName = (String)obj; + obj = entry.getValue(); + if( !(obj instanceof String) ) + throw new LuanException("zip file table values must be strings"); + String uriStr = (String)obj; + out.putNextEntry(new ZipEntry(fileName)); + obj = uri(luan,uriStr).rawGet("java"); + if( !(obj instanceof LuanIn) ) + throw new LuanException("invalid uri for zip"); + LuanIn zipIn = (LuanIn)obj; + InputStream in = zipIn.inputStream(); + Utils.copyAll(in,out); + in.close(); + out.closeEntry(); + } + out.close(); + } + + // security public interface Security {