How do I zip multiple directories and preserve structure

5 ビュー (過去 30 日間)
Michael
Michael 2013 年 9 月 13 日
コメント済み: per isakson 2016 年 10 月 1 日
Let's say I have 3 directories, each with their own contents (files and subdirectories).
C:\foo1\ D:\foo2\ E:\foo3\
I want to zip these up so that the zip file contains the following directory structure: \foo1\ \foo2\ \foo3\
So far, I've figured this out: zip(myzipfile.zip,{foo1_path,foo2_path,foo3_path});
However, this will put all the contents of each of the directories into the zip file - it does not preserve the directory structure of having the actual 'foo1' folder listed in the zip file.
This is a weird one, but due to various permissions issues and other IT issues, this is the only way I can automate my zip file logging...
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 9 月 13 日
You cannot invoke an external zip program?
Michael
Michael 2013 年 9 月 16 日
Nope, no external programs are available, and new programs cannot be installed.

サインインしてコメントする。

採用された回答

Jan
Jan 2013 年 9 月 15 日
編集済み: per isakson 2016 年 10 月 1 日
You can call the Java methods as in the zip() command. Some of the methods are not documented and might not work with future Matlab versions, but I have success with this from Matlab 6.5 to R2011b:
import java.io.*;
import java.util.zip.*;
fileOut = FileOutputStream('myFile.zip');
zipOut = ZipOutputStream(fileOut);
% This InterruptibleStreamCopier is unsupported and may change without notice.
StreamCopier = ...
com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
%
% You will need a loop over your filres from here...
fileIn = FileInputStream('C:\foo1\File1.txt');
zipObj = ZipEntry('foo1\File1.txt'); % <== relative path name
zipOut.putNextEntry(zipObj);
StreamCopier.copyStream(fileIn, zipOut);
fileIn.close;
zipOut.closeEntry;
% ...to here
%
% Close streams:
zipOut.close;
fileOut.close;
  2 件のコメント
Michael
Michael 2013 年 9 月 16 日
I implemented this in my code. It was kind of uglier than I would have liked, but it does work. I'm on R2012a -thanks!
per isakson
per isakson 2016 年 10 月 1 日
It works on R2016a.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by