# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1500453571 -7200
#      Wed Jul 19 10:39:31 2017 +0200
# Node ID 209f4788201886fc35f91f1944015257f766051f
# Parent  5b5b31fa402016d27ec8d6f9931e245419a85c80
Unzipped content instead of actual xlsx file in destination path (bug #51203).

__OCT_spsh_open__.m: Call InfoZip directly with absolute folders instead
of unpack.

__OCT_spsh_close__.m: Zip and move/rename in two steps.

diff -r 5b5b31fa4020 -r 209f47882018 inst/private/__OCT_spsh_close__.m
--- a/inst/private/__OCT_spsh_close__.m	Tue Jul 18 22:02:41 2017 +0200
+++ b/inst/private/__OCT_spsh_close__.m	Wed Jul 19 10:39:31 2017 +0200
@@ -47,14 +47,28 @@
             pause (0.05);
       ## Zip tmp directory into .ods or .xlsx and copy it over original file
       try
-        [stts, op] = system (sprintf ('zip -q -r "%s" *.* .', filename));
-        pause (0.20);
+        addToFile = exist (filename, "file");
+        if exist (fullfile (xls.workbook, [fname ".zip"]), "file")
+          delete (fullfile (xls.workbook, [fname ".zip"]));
+        endif
+        [stts, op] = system (sprintf ('zip -q -r "%s" *', [fname ".zip"]));
+
         if (stts == 0)
+          ## Move file from temp folder and rename
+          [mvst, msg] = movefile (["." filesep fname ".zip"], filename, "f");
+          if ~mvst
+            warning ('Moving file "%s" to "%s" failed with meassage: "%s"', ...
+                     fullfile (xls.workbook, [fname ".zip"]), filename, msg);
+          endif
           xls.changed = 0;
+        else
+          warning ("Zipping xlsx-file failed with with error %d\nOutput:\n%s\n", stts, op);
         endif
-      catch
-        printf ("odsclose: could not zip files in %s to %s\n", ...
+      catch err
+        printf ("xlsclose: could not zip files in %s to %s\n", ...
                 xls.workbook, filename);
+        warning (err.message)
+        warning (err.stack)
       end_try_catch;
     endif
 
diff -r 5b5b31fa4020 -r 209f47882018 inst/private/__OCT_spsh_open__.m
--- a/inst/private/__OCT_spsh_open__.m	Tue Jul 18 22:02:41 2017 +0200
+++ b/inst/private/__OCT_spsh_open__.m	Wed Jul 19 10:39:31 2017 +0200
@@ -61,9 +61,12 @@
   else
     ## xlsx and ods are zipped
     try
-      unpack (templ, tmpdir, "unzip");
-      ## Allow unpack dust to settle down (lazy write file I/O)
-      pause (0.2);
+      t_unpack = tic;
+      ## FIXME: Use "unpack" instead of system ("unzip...") when it is fixed
+      [stts, op] = system (sprintf ('unzip -o "%s" -d %s', templ, tmpdir));
+      if (stts != 0)
+        warning ("UnZip failed with error %d\nOutput:\n%s\n", stts, op);
+      endif
     catch err
       printf ("file %s couldn't be unpacked. Is it the proper file format?\n", filename);
       warning (err.message)
@@ -83,7 +86,9 @@
     ## https://github.com/markuman/xlsxread
 
     ## Get sheet names. Speeds up other functions a lot if we can do it here
-    fid = fopen (sprintf ('%s/xl/workbook.xml', tmpdir));
+    workbook_xml = sprintf ('%s/xl/workbook.xml', tmpdir);
+    fid = fopen (workbook_xml);
+    i_open = 1;
     if (fid < 0)
       ## File open error
       warning ("xls2open: file %s couldn't be unzipped\n", filename);
@@ -108,16 +113,16 @@
     xls.sheets.sh_names = cell2mat (regexp (sheets, '<sheet name="(.*?)"', "tokens"));
     xls.sheets.rid = str2double (cell2mat (regexp (sheets, ' r:id="rId(\d+)"', "tokens")));
     xls.sheets.sheetid = str2double (cell2mat (regexp (sheets, ' sheetId="(\d+)"', "tokens")));
-    
+
     ## read worksheet.xml.rels
     fid = fopen (sprintf ("%s/xl/_rels/workbook.xml.rels", tmpdir));
     if (fid > 0)
       str = fread (fid, "char=>char").';
       fclose (fid);
       xls.sheets.rels = ...
-       regexp (str, ['<Relationship Id="rId(\d+)" ' ...
-                     'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" ' ...
-                     'Target="worksheets/sheet(\d+).xml"[^/>]*/>'], "tokens");
+      regexp (str, ['<Relationship Id="rId(\d+)" ' ...
+                    'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" ' ...
+                    'Target="worksheets/sheet(\d+).xml"[^/>]*/>'], "tokens");
       xls.sheets.rels = reshape (str2double (cell2mat (xls.sheets.rels)), [], columns(xls.sheets.rels))';
     else
       xls.sheets.rels = reshape ([xls.sheets.rid xls.sheets.rid], [], columns (xls.sheets.rid));
