aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/autoload/zip.vim27
-rw-r--r--test/old/testdir/samples/testa.zipbin0 -> 1236 bytes
-rw-r--r--test/old/testdir/test_zip_plugin.vim104
3 files changed, 127 insertions, 4 deletions
diff --git a/runtime/autoload/zip.vim b/runtime/autoload/zip.vim
index d0d6863329..f113be6b6b 100644
--- a/runtime/autoload/zip.vim
+++ b/runtime/autoload/zip.vim
@@ -1,6 +1,6 @@
" zip.vim: Handles browsing zipfiles
" AUTOLOAD PORTION
-" Date: Aug 05, 2024
+" Date: Aug 18, 2024
" Version: 34
" Maintainer: This runtime file is looking for a new maintainer.
" Former Maintainer: Charles E Campbell
@@ -12,6 +12,7 @@
" 2024 Aug 04 by Vim Project: escape '[' in name of file to be extracted
" 2024 Aug 05 by Vim Project: workaround for the FreeBSD's unzip
" 2024 Aug 05 by Vim Project: clean-up and make it work with shellslash on Windows
+" 2024 Aug 18 by Vim Project: correctly handle special globbing chars
" License: Vim License (see vim's :help license)
" Copyright: Copyright (C) 2005-2019 Charles E. Campbell {{{1
" Permission is hereby granted to use and distribute this code,
@@ -72,6 +73,11 @@ if v:version < 702
call s:Mess('WarningMsg', "***warning*** this version of zip needs vim 7.2 or later")
finish
endif
+" sanity checks
+if !executable(g:zip_unzipcmd)
+ call s:Mess('Error', "***error*** (zip#Browse) unzip not available on your system")
+ finish
+endif
if !dist#vim#IsSafeExecutable('zip', g:zip_unzipcmd)
call s:Mess('Error', "Warning: NOT executing " .. g:zip_unzipcmd .. " from current directory!")
finish
@@ -198,7 +204,7 @@ fun! zip#Read(fname,mode)
let zipfile = substitute(a:fname,'^.\{-}zipfile://\(.\{-}\)::[^\\].*$','\1','')
let fname = substitute(a:fname,'^.\{-}zipfile://.\{-}::\([^\\].*\)$','\1','')
endif
- let fname = substitute(fname, '[', '[[]', 'g')
+ let fname = fname->substitute('[', '[[]', 'g')->escape('?*\\')
" sanity check
if !executable(substitute(g:zip_unzipcmd,'\s\+.*$','',''))
call s:Mess('Error', "***error*** (zip#Read) sorry, your system doesn't appear to have the ".g:zip_unzipcmd." program")
@@ -330,9 +336,24 @@ fun! zip#Extract()
call s:Mess('Error', "***error*** (zip#Extract) Please specify a file, not a directory")
return
endif
+ if filereadable(fname)
+ call s:Mess('Error', "***error*** (zip#Extract) <" .. fname .."> already exists in directory, not overwriting!")
+ return
+ endif
+ let target = fname->substitute('\[', '[[]', 'g')
+ if &shell =~ 'cmd' && (has("win32") || has("win64"))
+ let target = target
+ \ ->substitute('[?*]', '[&]', 'g')
+ \ ->substitute('[\\]', '?', 'g')
+ \ ->shellescape()
+ " there cannot be a file name with '\' in its name, unzip replaces it by _
+ let fname = fname->substitute('[\\?*]', '_', 'g')
+ else
+ let target = target->escape('*?\\')->shellescape()
+ endif
" extract the file mentioned under the cursor
- call system($"{g:zip_extractcmd} {shellescape(b:zipfile)} {shellescape(fname)}")
+ call system($"{g:zip_extractcmd} -o {shellescape(b:zipfile)} {target}")
if v:shell_error != 0
call s:Mess('Error', "***error*** ".g:zip_extractcmd." ".b:zipfile." ".fname.": failed!")
elseif !filereadable(fname)
diff --git a/test/old/testdir/samples/testa.zip b/test/old/testdir/samples/testa.zip
new file mode 100644
index 0000000000..10b0346e76
--- /dev/null
+++ b/test/old/testdir/samples/testa.zip
Binary files differ
diff --git a/test/old/testdir/test_zip_plugin.vim b/test/old/testdir/test_zip_plugin.vim
index b214516e05..a817d8371e 100644
--- a/test/old/testdir/test_zip_plugin.vim
+++ b/test/old/testdir/test_zip_plugin.vim
@@ -40,7 +40,8 @@ func Test_zip_basic()
\ execute("normal \<CR>"))
"## Check ENTER on file
- :1|:/^$//file/
+ :1
+ call search('file.txt')
exe ":normal \<cr>"
call assert_match('zipfile://.*/X.zip::Xzip/file.txt', @%)
call assert_equal('one', getline(1))
@@ -65,6 +66,10 @@ func Test_zip_basic()
:1|:/^$//file/
normal x
call assert_true(filereadable("Xzip/file.txt"))
+
+ "## Check not overwriting existing file
+ call assert_match('<Xzip/file.txt> .* not overwriting!', execute("normal x"))
+
call delete("Xzip", "rf")
"## Check extracting directory
@@ -131,5 +136,102 @@ func Test_zip_basic()
call assert_match('File not readable', execute("e Xnot_exists.zip"))
bw
+endfunc
+
+func Test_zip_glob_fname()
+ CheckNotMSWindows
+ " does not work on Windows, why?
+
+ "## copy sample zip file
+ if !filecopy("samples/testa.zip", "X.zip")
+ call assert_report("Can't copy samples/testa.zip")
+ return
+ endif
+ defer delete("X.zip")
+ defer delete('zipglob', 'rf')
+
+ e X.zip
+
+ "## 1) Check extracting strange files
+ :1
+ let fname = 'a[a].txt'
+ call search('\V' .. fname)
+ normal x
+ call assert_true(filereadable('zipglob/' .. fname))
+ call delete('zipglob', 'rf')
+
+ :1
+ let fname = 'a*.txt'
+ call search('\V' .. fname)
+ normal x
+ call assert_true(filereadable('zipglob/' .. fname))
+ call delete('zipglob', 'rf')
+
+ :1
+ let fname = 'a?.txt'
+ call search('\V' .. fname)
+ normal x
+ call assert_true(filereadable('zipglob/' .. fname))
+ call delete('zipglob', 'rf')
+
+ :1
+ let fname = 'a\.txt'
+ call search('\V' .. escape(fname, '\\'))
+ normal x
+ call assert_true(filereadable('zipglob/' .. fname))
+ call delete('zipglob', 'rf')
+
+ :1
+ let fname = 'a\\.txt'
+ call search('\V' .. escape(fname, '\\'))
+ normal x
+ call assert_true(filereadable('zipglob/' .. fname))
+ call delete('zipglob', 'rf')
+
+ "## 2) Check entering strange file names
+ :1
+ let fname = 'a[a].txt'
+ call search('\V' .. fname)
+ exe ":normal \<cr>"
+ call assert_match('zipfile://.*/X.zip::zipglob/a\[a\].txt', @%)
+ call assert_equal('a test file with []', getline(1))
+ bw
+
+ e X.zip
+ :1
+ let fname = 'a*.txt'
+ call search('\V' .. fname)
+ exe ":normal \<cr>"
+ call assert_match('zipfile://.*/X.zip::zipglob/a\*.txt', @%)
+ call assert_equal('a test file with a*', getline(1))
+ bw
+
+ e X.zip
+ :1
+ let fname = 'a?.txt'
+ call search('\V' .. fname)
+ exe ":normal \<cr>"
+ call assert_match('zipfile://.*/X.zip::zipglob/a?.txt', @%)
+ call assert_equal('a test file with a?', getline(1))
+ bw
+
+ e X.zip
+ :1
+ let fname = 'a\.txt'
+ call search('\V' .. escape(fname, '\\'))
+ exe ":normal \<cr>"
+ call assert_match('zipfile://.*/X.zip::zipglob/a\\.txt', @%)
+ call assert_equal('a test file with a\', getline(1))
+ bw
+ e X.zip
+ :1
+ let fname = 'a\\.txt'
+ call search('\V' .. escape(fname, '\\'))
+ exe ":normal \<cr>"
+ call assert_match('zipfile://.*/X.zip::zipglob/a\\\\.txt', @%)
+ call assert_equal('a test file with a double \', getline(1))
+ bw
+
+ bw
endfunc