as title says, trying create zip inside of zip.
i have zip archive created @ variable called "targetfilepath"
var projectdirectorypath = currentprojectviewmodel.directorypath; zipfile.createfromdirectory(projectdirectorypath, targetfilepath+@"/project.zip");
the above not work, assuming because path c:\test.zip\project.zip not valid path.
edit clarity: literally want zip files , place zip inside of archive. has using system.io.compression, no 3rd party methods.
from understanding have create target address zip files, and,also experience, target address cannot invalid path such "c:\test.zip\project.zip"
the easiest way create zip file in temporary file contents of projectdirectorypath
, once done add file target zip archive.
so this:
var projectdirectorypath = currentprojectviewmodel.directorypath; var tempfile = path.combine(path.gettemppath(), path.getrandomfilename()); try { zipfile.createfromdirectory(projectdirectorypath, tempfile); using (var archive = zipfile.open(targetfilepath, ziparchivemode.update)) { archive.createentryfromfile(tempfile, "project.zip", compressionlevel.nocompression); } } { system.io.file.delete(tempfile); }
i suggest use compressionlevel.nocompression
because zip archive compressed , layer of compression add no value it. using ziparchivemode.update
when opening archive important zip file altered. other values enumerator used create new archive scratch or open read-only.
in example used createentryfromfile
function. extension method make sure have namespace in using
declarations.
it possible implement in way no temporary file required, requires lot more code, have create new zip file , write directly stream when creating new entry in achive
. cases example gave should job.
Comments
Post a Comment