I actually managed to solve my own problem. The issue was that zip() doesn't keep directory structure if the file paths are absolute. So I added a few lines of code in the section that builds filearray to convert paths from absolute to relative. Now it works like a charm!
Zipping blocks of files while maintaining directory structure
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matthew
el 30 de Ag. de 2023
Respondida: Matthew
el 30 de Ag. de 2023
For reasons I won't get into, I need to transfer lots of data (hundreds of files) to a customer in .zip files with no more than 25 files each. I am trying to write a Matlab script to crawl through all the files, build a file list, and then zip the files in blocks of 25 while maintaining directory structure. The thought is the customer can then unzip the contents of all the .zip files into the same root directory (no matter how the files are broken up) and the directory structure is maintained. The problem with my current approach is that when I loop over the file list, directory structure is not maintained -- I end up with flat .zip files in 25 file blocks. I know I can maintain directory structure if I point the zip function to the file directories rather than the individual files, but that gets complicated real fast given there will likely be breaks between blocks within a folder and some folders will surely contain more than 25 files. I'm sure I could come up with some complicated script logic to solve my problem, but I'm hoping there is a much simpler solution -- is there a simple way with zip() syntax to say to Matlab, "please keep the directory structure associated with this particular file"?
% Build File List
filelist=dir(fullfile('.','**\*.*'))
filearray={};
jj=1;
for ii=1:length(filelist) % Loop over all files from dir
if filelist(ii).isdir~=1 % Remove directories so only file names are stored
filearray{jj}=fullfile(filelist(ii).folder,filelist(ii).name);
jj=jj+1;
end
end
% Zip files into blocks of blockSize
blockSize=25;
blockStart=1;
zipIndex=1;
zipTotal=ceil(length(filearray)/blockSize);
for kk=1:zipTotal
zipName=strcat('zip',sprintf('%g',zipIndex),'.zip'); % Current zip name
if kk~=zipTotal % If not final zip, which may have less than 25 files...
zip(zipName,filearray(blockStart:blockStart+blockSize-1));
else % If final zip...
zip(zipName,filearray(blockStart:end));
end
zipIndex=zipIndex+1;
blockStart=blockStart+blockSize;
end
0 comentarios
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!