[CSharp] Zip 파일 압축, 해제

using (ZipArchive zipArchive = ZipFile.OpenRead(LOCAL_FILE))
{
	foreach (ZipArchiveEntry zipArchiveEntry in zipArchive.Entries)
	{
		try
		{
			string folderPath = Path.GetDirectoryName(Path.Combine(LOCAL_ZIPARCHIVE_PATH, zipArchiveEntry.FullName));
			//
			if (!Directory.Exists(LOCAL_ZIPARCHIVE_PATH))
			{
				Directory.CreateDirectory(LOCAL_ZIPARCHIVE_PATH);
			}
			//
			zipArchiveEntry.ExtractToFile(Path.Combine(LOCAL_ZIPARCHIVE_PATH, zipArchiveEntry.FullName), true);
		}
		catch (PathTooLongException pt_ex)
		{
			Console.WriteLine(pt_ex);
		}
	}
}
					
using (FileStream fileStream = new FileStream(SFTP_DOWNLOAD_LOCAL_PATH, FileMode.Create, FileAccess.ReadWrite))
{
	using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Update))
	{
		try
		{
			foreach (string filePath in Directory.EnumerateFiles(DOWNLOAD_LOCAL_ZIPARCHIVE_PATH, "*.*", SearchOption.AllDirectories))
			{
				string relativePath = filePath.Substring(DOWNLOAD_LOCAL_ZIPARCHIVE_PATH.Length + 1);
				try
				{
					zipArchive.CreateEntryFromFile(filePath, relativePath);

				}
				catch (PathTooLongException pt_ex)
				{
					Console.WriteLine(pt_ex);
				}
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex);
		} 
		finally
		{
			zipArchive.Dispose();
			fileStream.Close();
		}
	}
}

이전글
Next Post Coming Soon...▶

답글 남기기

이매일 주소를 발행하지 않을 것입니다. 필수 항목은 *(으)로 표시합니다