如何在不使用Matlab命令提取的情况下重命名zip文件中的文件

问题描述

我有一堆压缩文件夹,我必须提取和读取数据(存储在一个唯一的文件中)。问题是这些文件夹中的某些文件夹有两个名称相同的错误(而不是1)错误的两个文件。当我使用Matlab命令“解压缩”时,其中一个文件被另一个覆盖。问题是这两个文件不相同:其中一个拥有我需要的信息,另一个几乎是空的。因此,我想将这两个文件重命名为file_a和file_b,将它们解压缩,然后将它们都解压缩,仅保留较大的一个

您知道是否可以重命名zip文件吗?

解决方法

我制作了一个函数,该函数将修改zip文件中的文件名,以使文件名看起来可以被无压缩地压缩。

该函数在zip文件中找到文件名,并用“ A,B,C,D等...”顺序更改它遇到的每个文件的首字母。

function differentiateFileNames(zipFilename)

    %% get the filenames contained in the zip file
    filenames = getZipFileNames(zipFilename) ;
    nFiles = numel(filenames) ;

    %% Find the positions of the file name fields
    % read the full file as a string
    str = fileread(zipFilename) ;
    % if all filenames are identical,we only need to search for the first name
    % in our list
    idx = strfind( str,filenames{1} ) ;

    %% group indices by physical file
    % Each filename appears twice in the zip file:
    % ex for 2 files: file1 ... file2 ... file1 ...file2
    idx = reshape(idx,nFiles,2)-1 ;

    %% Now modify each filename
    % (replace the first character of each filename)
    fid = fopen(zipFilename,'r+') ;

    for k=1:nFiles
        char2write = uint8('A'+(k-1)) ; % will be: A,B,C,D,ect ...
        fseek(fid,idx(k,1),'bof') ;
        fwrite(fid,char2write,'uint8') ;

        fseek(fid,2),'uint8') ;
    end

    fclose(fid) ;
end

function filenames = getZipFileNames(zipFilename)
    try
       % Create a Java file of the ZIP filename.
       zipJavaFile  = java.io.File(zipFilename);
       % Create a Java ZipFile and validate it.
       zipFile = org.apache.tools.zip.ZipFile(zipJavaFile);
       % Extract the entries from the ZipFile.
       entries = zipFile.getEntries;

    catch exception
       if ~isempty(zipFile)
           zipFile.close;
       end    
       delete(cleanUpUrl);
       error(message('MATLAB:unzip:invalidZipFile',zipFilename));
    end
    cleanUpObject = onCleanup(@()zipFile.close);

    k = 0 ;
    filenames = cell('') ;
    while entries.hasMoreElements
        k=k+1;
        filenames{k,1} = char(entries.nextElement.getName) ;
    end
    zipFile.close
end

请注意,此脚本假定所有文件在zip文件中均具有相似的名称。当找到文件名位置时,仅检查找到的第一个文件名。

子功能getZipFileNames只是unzip.m的一部分,只有必要的内容才能读取zip文件中包含的文件名。


用于测试: 我制作了一个包含2个文件的zip文件:

New Text Document1.txt
New Text Document2.txt

我使用十六进制编辑器修改了zip文件中的文件名,以便:

New Text Document1.txt
New Text Document1.txt

,因此两个文件在存档中的名称相同。如果我尝试解压缩该文件,如您所述,我只会在输出中得到一个文件(最后一个文件会覆盖另一个文件)。

如果我运行differentiateFileNames(zipFilename),然后解压缩该文件,则会在输出目录中获得2个文件:

Aew Text Document1.txt
Bew Text Document1.txt

我知道它看起来有些神秘,但是可以确保文件被区分。如果您愿意,作为一项练习,无需花费太多时间即可将脚本扩展为直接解压缩文件,找出最大的文件,删除另一个文件,然后使用适当的原始文件重命名文件名称。