Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Archive file or folder from target path
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static bool CreateZipFile(string path)
- {
- string temp = string.Empty;
- bool success = false;
- FileAttributes attributes = File.GetAttributes(path);
- if ((attributes & FileAttributes.Directory) != FileAttributes.Directory)
- {
- try
- {
- string pathToZip = Path.GetFileNameWithoutExtension(path);
- pathToZip = Path.GetDirectoryName(path) + "\" + pathToZip + ".zip";
- using (FileStream fs = File.Create(pathToZip))
- {
- using (ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Create))
- {
- ZipArchiveEntry entry = zip.CreateEntryFromFile(path, Path.GetFileName(path));
- }
- }
- success = true;
- }
- catch (Exception error)
- {
- success = false;
- LogWrite("ErrorZip.log", error.Message);
- }
- }
- else
- {
- try
- {
- temp = path + ".zip";
- ZipFile.CreateFromDirectory(path, temp);
- success = true;
- }
- catch (Exception error)
- {
- success = false;
- LogWrite("ErrorZip.log", error.Message);
- }
- }
- return success;
- }
Add Comment
Please, Sign In to add comment