Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Zipping files using the built in windows classes, by importing COM component

$
0
0
Hello, I am having a problem trying to zip a file, not really a problem, but I'm creating a backup program, and it imports the COM component: Microsoft Shell Controls And Automation, when I compile a .zip file, from a source folder, it copies ALL of the inside contents of that folder, into the zip file, I wanted it to copy the whole source folder, and paste it in there, otherwise, I would be getting clutter from all sorts of parent directories and sub folders.

Edit: If you want to use the code, you have to make a Properties.txt file, the first line is the source folder, second is the dest. file


Code Snippet


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Zipping
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void btnCompress_Click(object sender, EventArgs e)
{
//File format should be line by line
//1st line = src folder to compress
//2nd line = destination zip (ext. required)
string AttributesPath = (Application.StartupPath + "\\Properties.txt");
StreamReader reader = new StreamReader(AttributesPath);
string[ ] Attributes = new string[200];
int Counter = 0;
while (reader.EndOfStream == false)
{
Attributes[Counter] = reader.ReadLine(); //Src folder\file on element 0, Dest file on 1
// (zip file).
Counter++;
}
string destfile;//where to place
//the contents that were compressed.
//The Folder to compress
string srcfolder;
Counter = 0;
while (Counter < Attributes.Length & Attributes[Counter] != null)
{
srcfolder = Attributes[Counter];
Counter++;
destfile = Attributes[Counter];
Counter++;
if (File.Exists(destfile) == false)
{
FileStream fs = File.Create(destfile);
//Hex representation of an empty zip file (created by another program)
byte[ ] emptyzip = new byte[ ]{80,75,5,6,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
fs.Write(emptyzip, 0, emptyzip.Length);
fs.Flush();
fs.Close();
}
//Copy a folder and its contents into the newly created zip file
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFlder = sc.NameSpace(srcfolder);
Shell32.Folder DestFlder = sc.NameSpace(destfile);
Shell32.FolderItems items = SrcFlder.Items;
DestFlder.CopyHere( items, 20);

Thread.Sleep(4000);
}
}
}
}



Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>