changeset 747:d3a1e9a48a94

add unzip
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Jul 2016 20:03:01 -0600
parents 293c397e8dee
children de2418d11786
files core/src/luan/modules/IoLuan.java
diffstat 1 files changed, 20 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/IoLuan.java	Thu Jul 14 19:36:02 2016 -0600
+++ b/core/src/luan/modules/IoLuan.java	Thu Jul 14 20:03:01 2016 -0600
@@ -31,8 +31,9 @@
 import java.net.UnknownHostException;
 import java.util.Enumeration;
 import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
-import java.util.zip.ZipEntry;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -211,6 +212,21 @@
 			}
 		}
 
+		public void unzip(String path) throws IOException, LuanException {
+			ZipInputStream in = new ZipInputStream(new BufferedInputStream(inputStream()));
+			ZipEntry entry;
+			while( (entry = in.getNextEntry()) != null ) {
+				if( entry.isDirectory() )
+					continue;
+				File file = new File(path,entry.getName());
+				file.getParentFile().mkdirs();
+				OutputStream out = new FileOutputStream(file);
+				Utils.copyAll(in,out);
+				out.close();
+			}
+			in.close();
+		}
+
 		public LuanTable table() {
 			LuanTable tbl = new LuanTable();
 			try {
@@ -236,6 +252,9 @@
 				tbl.rawPut( "exists", new LuanJavaFunction(
 					LuanIn.class.getMethod( "exists" ), this
 				) );
+				tbl.rawPut( "unzip", new LuanJavaFunction(
+					LuanIn.class.getMethod( "unzip", String.class ), this
+				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}