hi all
i need to zip file with my application...but i cant..
i found a function CreateZipFile(that uses the SharpZipLib) which requires two parameters
1)string fileName that's the file i want to zipp
2)string[] SourceFiles -> here i have problem
when i try to call the CreateZipFile function i cant understand what parameter i have to pass:
CreateZipFile(fileName,??????)
i report all the code so maybe will be more simple
please help me!!!
thanks to all!!
bye
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
......
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private string GetInformation (string fileName)
{
string information = fileName + " exists\r\n\r\n";
information += "Created: " + File.GetCreationTime( fileName ) + "\r\n";
information += "Last modified: " + File.GetLastWriteTime( fileName ) + "\r\n";
information += "Last accessed: " + File.GetLastAccessTime( fileName ) + "\r\n" + "\r\n";
return information;
}
private void btnInfo_Click(object sender, System.EventArgs e)
{
string fileName;
fileName = txtInputZipFile.Text;
//MessageBox.Show(fileName);
if (File.Exists (fileName))
{
txtInfoFile.Text = GetInformation(fileName);
try
{
StreamReader stream = new StreamReader(fileName);
txtInfoFile.Text += stream.ReadToEnd();
}
catch (IOException)
{
MessageBox.Show("File Error","File Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show( txtInputZipFile.Text + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
private void btnZip_Click(object sender, System.EventArgs e)
{
string fileName;
fileName = txtInputZipFile.Text;
//string[] filenames = Directory.GetFiles("*.mp3");
CreateZipFile(fileName,??????);
}
public void CreateZipFile(string ZipFileName, string[] SourceFiles)
{
// stream di scrittura dello ZIP
ZipOutputStream strmZipOutputStream = new ZipOutputStream(File.Create(ZipFileName));
// Definisco il CRC32
Crc32 objCrc32 = new Crc32();
// livello di compressione
// 0: no compression (store)
// 9: maximum compression
strmZipOutputStream.SetLevel(7);
// scorre l'array con i filenames dei files sorgenti
foreach (string file in SourceFiles)
{
// nuova entry nel file ZIP
ZipEntry newZIPEntry = new ZipEntry(file);
// apre il file sorgente in lettura
FileStream strmFile = File.OpenRead(file);
// bytes da leggere e già letti
int bytesRead;
// definisco un buffer per la lettura da 200000 bytes
byte[] mBuffer = new byte[200000];
// azzera il CRC
objCrc32.Reset();
// inserisce l'entry nello ZIP
strmZipOutputStream.PutNextEntry(newZIPEntry);
// ciclo per la lettura del file sorgente
while (strmFile.Position < strmFile.Length)
{
bytesRead = strmFile.Read(mBuffer, 0, mBuffer.Length);
strmZipOutputStream.Write(mBuffer, 0, bytesRead);
objCrc32.Update(mBuffer, 0, bytesRead);
}
// imposta il CRC del nuovo file all'interno dello ZIP
newZIPEntry.Crc = objCrc32.Value;
// imposta le caratteristiche del nuovo file
newZIPEntry.DateTime = File.GetCreationTime(fileName);
newZIPEntry.Size = strmFile.Length;
// chiude il file sorgente
strmFile.Close();
}
// chiude il file ZIP
strmZipOutputStream.Finish();
strmZipOutputStream.Close();
}
}
}
i need to zip file with my application...but i cant..
i found a function CreateZipFile(that uses the SharpZipLib) which requires two parameters
1)string fileName that's the file i want to zipp
2)string[] SourceFiles -> here i have problem
when i try to call the CreateZipFile function i cant understand what parameter i have to pass:
CreateZipFile(fileName,??????)
i report all the code so maybe will be more simple
please help me!!!
thanks to all!!
bye
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
......
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private string GetInformation (string fileName)
{
string information = fileName + " exists\r\n\r\n";
information += "Created: " + File.GetCreationTime( fileName ) + "\r\n";
information += "Last modified: " + File.GetLastWriteTime( fileName ) + "\r\n";
information += "Last accessed: " + File.GetLastAccessTime( fileName ) + "\r\n" + "\r\n";
return information;
}
private void btnInfo_Click(object sender, System.EventArgs e)
{
string fileName;
fileName = txtInputZipFile.Text;
//MessageBox.Show(fileName);
if (File.Exists (fileName))
{
txtInfoFile.Text = GetInformation(fileName);
try
{
StreamReader stream = new StreamReader(fileName);
txtInfoFile.Text += stream.ReadToEnd();
}
catch (IOException)
{
MessageBox.Show("File Error","File Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show( txtInputZipFile.Text + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
}
private void btnZip_Click(object sender, System.EventArgs e)
{
string fileName;
fileName = txtInputZipFile.Text;
//string[] filenames = Directory.GetFiles("*.mp3");
CreateZipFile(fileName,??????);
}
public void CreateZipFile(string ZipFileName, string[] SourceFiles)
{
// stream di scrittura dello ZIP
ZipOutputStream strmZipOutputStream = new ZipOutputStream(File.Create(ZipFileName));
// Definisco il CRC32
Crc32 objCrc32 = new Crc32();
// livello di compressione
// 0: no compression (store)
// 9: maximum compression
strmZipOutputStream.SetLevel(7);
// scorre l'array con i filenames dei files sorgenti
foreach (string file in SourceFiles)
{
// nuova entry nel file ZIP
ZipEntry newZIPEntry = new ZipEntry(file);
// apre il file sorgente in lettura
FileStream strmFile = File.OpenRead(file);
// bytes da leggere e già letti
int bytesRead;
// definisco un buffer per la lettura da 200000 bytes
byte[] mBuffer = new byte[200000];
// azzera il CRC
objCrc32.Reset();
// inserisce l'entry nello ZIP
strmZipOutputStream.PutNextEntry(newZIPEntry);
// ciclo per la lettura del file sorgente
while (strmFile.Position < strmFile.Length)
{
bytesRead = strmFile.Read(mBuffer, 0, mBuffer.Length);
strmZipOutputStream.Write(mBuffer, 0, bytesRead);
objCrc32.Update(mBuffer, 0, bytesRead);
}
// imposta il CRC del nuovo file all'interno dello ZIP
newZIPEntry.Crc = objCrc32.Value;
// imposta le caratteristiche del nuovo file
newZIPEntry.DateTime = File.GetCreationTime(fileName);
newZIPEntry.Size = strmFile.Length;
// chiude il file sorgente
strmFile.Close();
}
// chiude il file ZIP
strmZipOutputStream.Finish();
strmZipOutputStream.Close();
}
}
}