Unzip с заменой linux

How to update one file in a zip archive

Is it possible to replace a file in a zip file without unzipping? The file to update is an XML file that resides in a huge zip archive. To update this XML file, I have to unzip the archive, delete the old XML file, add the new one and then rezip. This takes a considerable amount of time. So want to be able to replace that one XML through a script. I already have the one that checks for updates on the XML I have. using zip command
Sorry, I would use the zip command to do things like that but the problem is the script is actually for an android phone and zip is not a command I can use unfortunately sorry I left that out. I would have used zip definitely if i could but I only have unzip for droid and then there is tar in busybox but tar doesn’t do what I need

10 Answers 10

zip [zipfile] [file to update] 
$ zip test.zip test/test.txt updating: test/test.txt (stored 0%) 

Take into account that you should see updating: instead of adding: if you want to update a file inside the zip; otherwise you are adding files to the zip, not replacing it.

For me, what this does is add the entire test/test.txt file inside the root folder of test.zip . Do you know how to specify the folder where the test.txt file will be inside? How can I add the content of the test folder and not the folder itself?

cd into the test directory and zip ../test.zip test.txt to put test.txt at the root of the zip. If you want a different named directory make it and put test.txt in there and add as above.

Источник

Как перезаписать только измененные файлы, Unzip Linux

Как сделать, чтобы при распаковке архива, он заменял только измененные файлы. Сейчас строка выглядит так unzip -o # -d # Префикс -o делает перезапись всех файлов без вопроса.

1 ответ 1

в программу unzip , насколько мне известно, не вложена требуемая функциональность.

Читайте также:  1с бухгалтерия клиент linux

если места для распаковки архива в другой каталог достаточно, то можно воспользоваться (после распаковки) программой rsync.

$ mkdir src $ touch src/file1 $ zip -r 1.zip src adding: src/ (stored 0%) adding: src/file1 (stored 0%) 

теперь распакуем во временный каталог:

$ unzip 1.zip -d temp Archive: 1.zip creating: temp/src/ extracting: temp/src/file1 

и запустим rsync с опцией -n (dry run), чтобы не копировать, а только посмотреть, что будет копироваться. как видим, rsync не будет копировать ничего:

$ rsync -n -av temp/src/ src/ sending incremental file list sent 64 bytes received 12 bytes 152.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN) 

а если файл в исходном каталоге изменён, то rsync его перезапишет:

$ touch src/file1 $ rsync -n -av temp/src/ src/ sending incremental file list file1 sent 67 bytes received 15 bytes 164.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN) 

для полной синхронизации, возможно, имеет смысл воспользоваться ещё и опцией —delete — с нею rsync удалит те файлы, которых нет в источнике (каталоге temp/src ). продолжая вышеприведённый пример:

$ touch src/file2 $ rsync -n -av --delete temp/src/ src/ sending incremental file list deleting file2 ./ file1 sent 62 bytes received 27 bytes 178.00 bytes/sec total size is 0 speedup is 0.00 (DRY RUN) 

появившаяся в выводе строка ./ означает, что у каталога src будет восстановлено время его последнего изменения (время изменилось на текущее после выполнения команды touch src/file2 ).

Источник

Extracting zip from Terminal with renaming when filename exists?

I am following this question to extract a zip. Is there a way to automatically add a number after the file extracted when there is an existing file with the same name? For example, I have example.zip with a file called contents.csv . My directory is this:

$ unzip example.zip Archive: example.zip replace contents.csv? [y]es, [n]o, [A]ll, [N]one, [r]ename: y 

Instead, I just want to run unzip example.zip then have a number ( -1 ) automatically appended to the file name in the case of a clash like this:

contents-1.csv contents.csv example.zip 
contents-2.csv contents-1.csv contents.csv example.zip 

1 Answer 1

You can do it with the -B switch.

 -B [when compiled with UNIXBACKUP defined] save a backup copy of each overwritten file. The backup file is gets the name of the target file with a tilde and optionally a unique sequence number (up to 5 digits) appended. The sequence number is applied when‐ ever another file with the original name plus tilde already exists. When used together with the "overwrite all" option -o, numbered backup files are never created. In this case, all backup files are named as the original file with an appended tilde, existing backup files are deleted without notice. This feature works similarly to the default behavior of emacs(1) in many locations. Example: the old copy of ``foo'' is renamed to ``foo~''. Warning: Users should be aware that the -B option does not pre‐ vent loss of existing data under all circumstances. For exam‐ ple, when unzip is run in overwrite-all mode, an existing ``foo~'' file is deleted before unzip attempts to rename ``foo'' to ``foo~''. When this rename attempt fails (because of a file locks, insufficient privileges, or . ), the extraction of ``foo~'' gets cancelled, but the old backup file is already lost. A similar scenario takes place when the sequence number range for numbered backup files gets exhausted (99999, or 65535 for 16-bit systems). In this case, the backup file with the maximum sequence number is deleted and replaced by the new backup version without notice. 
terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg 20161225_150835.jpg 98percenteffective.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg 20170204_193007.jpg images.jpg 20170204_193309.jpg img_7230.jpg 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170401_200805.jpg unnamed.jpg -------------------------------------------------------------- 06/27/17 20:43:56 terrance@terrance-ubuntu:~/testdir$ zip newfiles.zip * adding: 20161203_202646.jpg (deflated 1%) adding: 20161225_150835.jpg (deflated 0%) adding: 20170204_192943.jpg (deflated 0%) adding: 20170204_193007.jpg (deflated 0%) adding: 20170204_193309.jpg (deflated 0%) adding: 20170204_194206.jpg (deflated 3%) adding: 20170322_230834.jpg (deflated 1%) adding: 20170401_200805.jpg (deflated 0%) adding: 20170610_110407-PANO.jpg (deflated 1%) adding: 98percenteffective.jpg (deflated 1%) adding: Home_Is_dog_hair.jpg (deflated 4%) adding: images.jpg (deflated 0%) adding: img_7230.jpg (deflated 0%) adding: ps_2013_12_29___11_47_19_fixed.jpg (deflated 1%) adding: ps_2013_12_29___11_47_19.jpg (deflated 0%) adding: unnamed.jpg (deflated 0%) -------------------------------------------------------------- 06/27/17 20:44:03 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:12 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170610_110407-PANO.jpg -------------------------------------------------------------- 06/27/17 20:44:13 terrance@terrance-ubuntu:~/testdir$ unzip -B newfiles.zip Archive: newfiles.zip inflating: 20161203_202646.jpg inflating: 20161225_150835.jpg inflating: 20170204_192943.jpg inflating: 20170204_193007.jpg inflating: 20170204_193309.jpg inflating: 20170204_194206.jpg inflating: 20170322_230834.jpg inflating: 20170401_200805.jpg inflating: 20170610_110407-PANO.jpg inflating: 98percenteffective.jpg inflating: Home_Is_dog_hair.jpg inflating: images.jpg inflating: img_7230.jpg inflating: ps_2013_12_29___11_47_19_fixed.jpg inflating: ps_2013_12_29___11_47_19.jpg inflating: unnamed.jpg -------------------------------------------------------------- 06/27/17 20:44:16 terrance@terrance-ubuntu:~/testdir$ ls 20161203_202646.jpg 20170610_110407-PANO.jpg~ 20161203_202646.jpg~ 20170610_110407-PANO.jpg~1 20161203_202646.jpg~1 98percenteffective.jpg 20161225_150835.jpg 98percenteffective.jpg~ 20161225_150835.jpg~ 98percenteffective.jpg~1 20161225_150835.jpg~1 Home_Is_dog_hair.jpg 20170204_192943.jpg Home_Is_dog_hair.jpg~ 20170204_192943.jpg~ Home_Is_dog_hair.jpg~1 20170204_192943.jpg~1 images.jpg 20170204_193007.jpg images.jpg~ 20170204_193007.jpg~ images.jpg~1 20170204_193007.jpg~1 img_7230.jpg 20170204_193309.jpg img_7230.jpg~ 20170204_193309.jpg~ img_7230.jpg~1 20170204_193309.jpg~1 newfiles.zip 20170204_194206.jpg ps_2013_12_29___11_47_19_fixed.jpg 20170204_194206.jpg~ ps_2013_12_29___11_47_19_fixed.jpg~ 20170204_194206.jpg~1 ps_2013_12_29___11_47_19_fixed.jpg~1 20170322_230834.jpg ps_2013_12_29___11_47_19.jpg 20170322_230834.jpg~ ps_2013_12_29___11_47_19.jpg~ 20170322_230834.jpg~1 ps_2013_12_29___11_47_19.jpg~1 20170401_200805.jpg unnamed.jpg 20170401_200805.jpg~ unnamed.jpg~ 20170401_200805.jpg~1 unnamed.jpg~1 20170610_110407-PANO.jpg 

Источник

Читайте также:  Восстановить загрузчик windows 10 через linux

Unzip to a folder with the same name as the file (without the .zip extension)

How to unzip a file (ex: foo.zip ) to a folder with the same name ( foo/ )? Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That’s how Mac’s unzip utility works and I want to do the same in CLI.

@Christopher I don’t see how it’s a duplicate. Can you find an answer to my question in that question?

@Fabby, no it doesn’t, I need a dynamic solution that won’t require adding the name of the target folder. I believe the second paragraph makes this very clear.

3 Answers 3

I use unar for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:

You can force the creation of a directory in all cases with the -d option:

Alternatively, a function can do this with unzip :

line removes the .zip extension, with no regard for anything else (so foo.zip becomes foo , and ~/foo.zip becomes ~/foo ). The

parameter expansion removes anything up to the last / , so ~/foo becomes foo . This means that the function extracts any .zip file to a directory named after it, in the current directory. Use unzip $1 -d «$» if you want to extract the archive to a directory alongside it instead.

unar is available for macOS (along with its GUI application, The Unarchiver), Windows, and Linux; it is packaged in many distributions, e.g. unar in Debian and derivatives, Fedora and derivatives, community/unarchiver in Arch Linux.

Читайте также:  Which linux 32 bit or 64 bit

Источник

Оцените статью
Adblock
detector