#!/bin/bash
strings -t d test.zip | \
grep '^\s\+[[:digit:]]\+\sAAAAA-\w\+\.txt' | \
sed 's/^\s\+\([[:digit:]]\+\)\s\(AAAAA\)\(-\w\+\.txt\).*$/\1 \2\3 BBBBB\3/g' | \
while read -a line; do
line_nbr=${line[0]};
fname=${line[1]};
new_name=${line[2]};
len=${#fname};
# printf "line: "$line_nbr"\nfile: "$fname"\nnew_name: "$new_name"\nlen: "$len"\n";
dd if=<(printf $new_name"\n") of=test.zip bs=1 seek=$line_nbr count=$len conv=notrunc
done;
输出
$ ls
AAAAA-apple.txt AAAAA-orange.txt zip_rename.sh
$ zip test.zip AAAAA-apple.txt AAAAA-orange.txt
adding: AAAAA-apple.txt (stored 0%)
adding: AAAAA-orange.txt (stored 0%)
$ ls
AAAAA-apple.txt AAAAA-orange.txt test.zip zip_rename.sh
$ ./zip_rename.sh
15+0 records in
15+0 records out
15 bytes (15 B) copied, 0.000107971 s, 139 kB/s
16+0 records in
16+0 records out
16 bytes (16 B) copied, 0.000109581 s, 146 kB/s
15+0 records in
15+0 records out
15 bytes (15 B) copied, 0.000150529 s, 99.6 kB/s
16+0 records in
16+0 records out
16 bytes (16 B) copied, 0.000101685 s, 157 kB/s
$ unzip test.zip
Archive: test.zip
extracting: BBBBB-apple.txt
extracting: BBBBB-orange.txt
$ ls
AAAAA-apple.txt BBBBB-apple.txt test.zip
AAAAA-orange.txt BBBBB-orange.txt zip_rename.sh
$ diff -qs AAAAA-apple.txt BBBBB-apple.txt
Files AAAAA-apple.txt and BBBBB-apple.txt are identical
$ diff -qs AAAAA-orange.txt BBBBB-orange.txt
Files AAAAA-orange.txt and BBBBB-orange.txt are identical
import os
from zipfile import ZipFile
import shutil
# rename the individual file and return the absolute path of the renamed file
def renameFile(root, fileName):
toks = fileName.split('.')
newName = toks[0].lower()+'.'+toks[1]
renamedPath = os.path.join(root,newName)
os.rename(os.path.join(root,fileName),renamedPath)
return renamedPath
def renameFilesInZip(filename):
# Create <filename_without_extension>_renamed folder to extract contents into
head, tail = os.path.split(filename)
newFolder = tail.split('.')[0]
newFolder += '_renamed'
newpath = os.path.join(head, newFolder)
if(not os.path.exists(newpath)):
os.mkdir(newpath)
# extracting contents into newly created folder
print("Extracting files\n")
try:
with ZipFile(filename, 'r', allowZip64=True) as zip_ref:
zip_ref.extractall(newpath)
zip_ref.close()
except ResponseError as err:
print(err)
return -1
# track the files that need to be repackaged in the zip with renamed files
filesToPackage = []
for r, d, f in os.walk(newpath):
for item in f:
# filter the file types that need to be renamed
if item.lower().endswith(('.jpg', '.png')):
# renaming file
renamedPath = renameFile(r, item)
filesToPackage.append(renamedPath)
else:
filesToPackage.append(os.path.join(r,item))
# creating new zip file
print("Writing renamed file\n")
zipObj = ZipFile(os.path.join(head, newFolder)+'.zip', 'w', allowZip64 = True)
for file in filesToPackage:
zipObj.write(file, os.path.relpath(file, newpath))
zipObj.close()
# removing extracted contents and origianl zip file
print('Cleaning up \n')
shutil.rmtree(newpath)
os.remove(filename)
# renaming zipfile with renamed contents to original zipfile name
os.rename(os.path.join(head, newFolder)+'.zip', filename)
使用以下代码为多个zip文件调用重命名函数
if __name__ == '__main__':
# zipfiles with contents to be renamed
zipFiles = ['/usr/app/data/mydata.zip', '/usr/app/data/mydata2.zip']
# do the renaming for all files one by one
for _zip in zipFiles:
renameFilesInZip(_zip)
3条答案
按热度按时间k3bvogb11#
计划
备份你的zip文件,然后再尝试这个
zip_rename.sh
输出
kgqe7b3p2#
7z命令
rn
重命名:7z rn arr.zip old new
rekjcdws3#
我们使用python
1.将
xyz.zip
文件解压缩到xyz_renamed/
文件夹1.在
renameFile
函数中重命名所需的文件(我们只重命名jpeg和png图像)(我们将名称更改为更低)1.将重命名的内容再次打包到
xyz_renamed.zip
中1.删除
xyz_renamed/
文件夹和xyz.zip
文件1.将
xyz_renamed.zip
重命名为xyz.zip
使用以下代码为多个zip文件调用重命名函数