Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help ZIPFILE!!!

Status
Not open for further replies.

alecomo

Programmer
Oct 28, 2005
4
IT
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();
}
}



}
 
You wrote
1)string fileName that's the file i want to zipp
2)string[] SourceFiles -> here i have problem

I think
1)string fileName of OUTPUT ZIP file
2)string[] SourceFiles -> array of filenames of all INPUT files to ZIP into 1)

- free online Compare/Diff of snippets
 
yeah its possible...so how can i tell to CreateZipFile which file I want to compress?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top