如何从MATLAB中的zip存档中提取单个文件?

thy

单文件 unzip

unzip()MATLAB中函数仅提供输入zip文件位置和输出目录位置的功能。有没有一种方法可以从zip存档中仅提取一个文件,而不是所有文件?

如果特定文件是已知文件,并且是唯一需要的文件,则这将减少提取文件的时间。

thy

MATLAB的 unzip

使用标准的MATLAB函数是不可能的,但是...让该函数破解以使其能够执行所需的操作!

MATLAB的unzip单文件黑客

使用来自MATLABunzip()的代码extractArchive()(从中调用unzip()),可以创建一个自定义函数以仅从zip存档中提取单个文件。

function [] = extractFile(zipFilename, outputDir, outputFile)
% extractFile

% Obtain the entry's output names
outputName = fullfile(outputDir, outputFile);

% Create a stream copier to copy files.
streamCopier = ...
    com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;

% Create a Java zipFile object and obtain the entries.
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);

    % Get entry
    entry = zipFile.getEntry(outputFile);

catch exception
    if ~isempty(zipFile)
        zipFile.close;
    end
    delete(cleanUpUrl);
    error(message('MATLAB:unzip:unvalidZipFile', zipFilename));
end

% Create the Java File output object using the entry's name.
file = java.io.File(outputName);

% If the parent directory of the entry name does not exist, then create it.
parentDir = char(file.getParent.toString);
if ~exist(parentDir, 'dir')
    mkdir(parentDir)
end

% Create an output stream
try
    fileOutputStream = java.io.FileOutputStream(file);
catch exception
    overwriteExistingFile = file.isFile && ~file.canWrite;
    if overwriteExistingFile
        warning(message('MATLAB:extractArchive:UnableToOverwrite', outputName));
    else
        warning(message('MATLAB:extractArchive:UnableToCreate', outputName));
    end
    return
end

% Create an input stream from the API
fileInputStream = zipFile.getInputStream(entry);

% Extract the entry via the output stream.
streamCopier.copyStream(fileInputStream, fileOutputStream);

% Close the output stream.
fileOutputStream.close;

end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从S3中的zip存档中提取文件

如何从cpio存档中提取单个文件?

如何从远程存档文件中提取单个文件?

使用密码从ZIP存档中提取文件

从zip存档中提取gzip文件

从 zip 存档中提取文件是否比从同一存档中复制文件更快?

以编程方式从7zip存档中提取单个特定文件-Java-Linux

如何从python中的.tar存档中提取特定文件?

如何从tar存档中提取特定文件?

如何从大型tar.gz存档中提取单个文件夹?

从Zip存档中提取图像

如何仅从ZIP存档中提取mp3文件

如果使用 setEncryptionName 加密文件,如何在 PHP 中提取 zip 存档

从Python3中的zip存档中提取特定文件夹的内容

无法从Mac中的zip存档中提取可执行文件

从zip存档中提取文件,然后重命名

如何从bash中的.rar中提取单个文件

当该文件不在中央目录中但具有自己的LFH时,是否有工具可以从ZIP存档中提取文件?

从zip归档文件中提取单个文件,而无需遍历Python中的整个名称列表

如何从MATLAB中的excel文件中提取元素?

如何仅从存档中提取一种文件?

从.tar.bz2存档中的文件中提取行

从python中的tar存档中提取压缩的gz文件

如何从zip内存中提取文件?

如何在zip存档中重命名/提取带有长名称的文件

如何提取添加到zip存档中的最后一个文件

将单个文件复制到zip存档中

在Linux上从zip存档中删除单个文件

使用7-zip从每个存档中批量提取文件