ในการ List ไฟล์ใน Zip
private void button_open_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
string zipPath = label1.Text;
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
// Gets the full path to ensure that relative segments are removed.
string destinationPath = entry.FullName;
listBox1.Items.Add(destinationPath);
}
}
}ในการ Extract File ออกมา แล้ว edit ด้วย Notepad
private void button_Edit_Click(object sender, EventArgs e)
{ // select file
int index = listBox1.SelectedIndex;
string sFile = listBox1.Items[index].ToString();
// extract file
string exPath = "c:/temp/";
string zipPath = label1.Text;
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
// Gets the full path to ensure that relative segments are removed.
string destinationPath = exPath+entry.FullName;
if (entry.FullName == sFile)
{ // extrac file
entry.ExtractToFile(destinationPath,true);
label_msg.Text = "Extract to " + destinationPath;
string editor = @"C:\WINDOWS\NOTEPAD.EXE";
Process.Start(editor, destinationPath);
}
}
}
}
ในการ Delete File เดิม และ จัดเก็บ
private void button_Save_Click(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
string sFile = listBox1.Items[index].ToString();
// extract file
string exPath = "c:/temp/";
string zipPath = label1.Text;
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
// delete old file
foreach (ZipArchiveEntry entry in archive.Entries)
{
// Gets the full path to ensure that relative segments are removed.
string destinationPath1 = exPath + entry.FullName;
if (entry.FullName == sFile)
{ // extrac file
entry.Delete();
label_msg.Text = "delete zip " + sFile;
break;
}
}
}
// update
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
string destinationPath = exPath + sFile;
archive.CreateEntryFromFile(destinationPath, sFile);
label_msg.Text = "Zip to " + destinationPath;
return;
}
}