changeset 746:293c397e8dee

improve zip
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Jul 2016 19:36:02 -0600
parents 9c1f28b26395
children d3a1e9a48a94
files core/src/luan/modules/Io.luan core/src/luan/modules/IoLuan.java lucene/src/luan/modules/lucene/Lucene.luan
diffstat 3 files changed, 45 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r 9c1f28b26395 -r 293c397e8dee core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Wed Jul 13 21:27:23 2016 -0600
+++ b/core/src/luan/modules/Io.luan	Thu Jul 14 19:36:02 2016 -0600
@@ -8,7 +8,6 @@
 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)
diff -r 9c1f28b26395 -r 293c397e8dee core/src/luan/modules/IoLuan.java
--- 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
 
diff -r 9c1f28b26395 -r 293c397e8dee lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Wed Jul 13 21:27:23 2016 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Thu Jul 14 19:36:02 2016 -0600
@@ -5,8 +5,10 @@
 local type = Luan.type or error()
 local Html = require "luan:Html.luan"
 local Io = require "luan:Io.luan"
+local uri = Io.uri or error()
 local String = require "luan:String.luan"
 local matches = String.matches or error()
+local Table = require "luan:Table.luan"
 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex"
 local NumberFieldParser = require "java:luan.modules.lucene.queryparser.NumberFieldParser"
 local StringFieldParser = require "java:luan.modules.lucene.queryparser.StringFieldParser"
@@ -105,9 +107,10 @@
 		index.snapshot( function(dir,file_names)
 			local t = {}
 			for _, file_name in ipairs(file_names) do
-				t[file_name] = "file:"..dir.."/"..file_name
+				t[#t+1] = dir.."/"..file_name
 			end
-			Io.zip(zip_file,t)
+			local base = uri("file:"..dir).parent().to_string()
+			uri(zip_file).zip(base,Table.unpack(t))
 		end )
 	end