Reputation: 361
I am trying to archive the content of a folder into a zip file.. the content are mostly large images. I am trying to do this using this code:
var folderPicker =
new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFile zipFile = await folder.CreateFileAsync(ThemeName.Text + ".zip",
Windows.Storage.CreationCollisionOption.GenerateUniqueName);
Stream themeZipFile = await zipFile.OpenStreamForWriteAsync();
ZipArchive archive = new ZipArchive(themeZipFile);
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder temp = await localFolder.GetFolderAsync("Temp");
var files = await temp.GetFilesAsync();
foreach (var item in files)
{
archive.CreateEntryFromFile(item.Path, item.Name);
}
}
However, when executed, I receive an error:
IOException: Cannot seek to an absolute stream position that is negative.
Now when I try this code:
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder =
await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
StorageFile zipFile =
await folder.CreateFileAsync(ThemeName.Text + ".zip", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await Task.Run(() => ZipFile.CreateFromDirectory(localFolder.Path, zipFile.Path));
}
I get that access is denied to whatever folder I pick ! How can I get around this to combine several larger files into one archive ?
Upvotes: 1
Views: 347
Reputation: 32775
How to archive several files in one ZIP file?
CreateFromDirectory
method will create zip file with second destinationArchiveFileName
paramater. So you need not create zip file in advance. You could use the follwing code to zip your folder directly.
if (ZipFolder != null)
{
// Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", ZipFolder);
await Task.Run(() =>
{
try
{
ZipFile.CreateFromDirectory(ApplicationData.Current.LocalFolder.Path, $"{ZipFolder.Path}\\{Guid.NewGuid()}.zip");
Debug.WriteLine("folder zipped");
}
catch (Exception w)
{
Debug.WriteLine(w);
}
});
}
Upvotes: 1
Reputation: 147
If creating a temp folder to archive the whole folder is a solution for you try this:
using System.IO.Compression;
var files = System.IO.Directory.EnumerateFiles(string PATH, ".jpeg", System.IO.SearchOption.AllDirectories)
foreach (var file in files)
{
File.Copy(file, tempPath + @"\" + System.IO.Path.GetFileName(file));
}
ZipFile.CreateFromDirectory(tempPath, zipPath, CompressionLevel.Fastest, true);
Directory.Delete(tempPath, true); //delete tempfolder after compress commplete
Upvotes: 1
Reputation: 1124
Using this method, you are able to create a ZIP file starting from a source directory. Here some Docs by Microsoft: ZIPFile.CreateFromDirectory
You can find the mentioned class and method in this namespace: System.IO.Compression
Upvotes: 1