class ZipLib
{
//파일 경로 가져오기
public static string[] GetFileList(string path)
{
string[] files = Directory.GetFiles(path);
string[] directories = Directory.GetDirectories(path);
if (directories.Length > 0)
{
foreach (string directory in directories)
{
string[] subfiles = GetFileList(directory); //재귀
if (subfiles.Length > 0)
{
int len = files.Length;
string[] newlist = new string[len + subfiles.Length];
files.CopyTo(newlist, 0);
for (int i = 0; i < subfiles.Length; i++)
{
newlist[len + i] = subfiles[i];
}
files = newlist;
}
}
}
return files;
}
// 압축하기
public static void Zip(string TargetFolder, string ZipName)
{
string[] FileNames = GetFileList(TargetFolder);
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(ZipName));
s.SetLevel(9);
foreach (string file in FileNames)
{
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file.Replace(TargetFolder, ""));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();
}
//압축 해제
public static void Unzip(string ZipName, string SavePath)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(ZipName));
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string FullName = SavePath + "\\" + theEntry.Name;
string DirName = Path.GetDirectoryName(FullName);
string FileName = Path.GetFileName(FullName);
if (!Directory.Exists(DirName)) Directory.CreateDirectory(DirName);
if (FileName != String.Empty)
{
FileStream SW = File.Create(FullName);
int Size = 2048;
byte[] data = new byte[2048];
while (true)
{
Size = s.Read(data, 0, data.Length);
if (Size > 0) SW.Write(data, 0, Size);
else break;
}
SW.Close();
}
}
s.Close();
}
}
댓글