Non è una cosa semplice. Comunque, vedi se possa andare.
Prima di tutto ti descrivo l'esempio che ho fatto nel mio computer.
Ho una directory virtuale chiamata prova, in pratica il progetto di asp.
Lì creo una directory chiamata Bin.
Dentro Bin scarico la dll (che non deve essere registrata) da questo indirizzo:
http://web.tiscali.it/archivio_esemp...harpZipLib.zip
Naturalmente l'ho messa là per comodità mia, ma il sito originale è alla prima voce di google.
Adesso, dentro prova, metto una pagina di prova asp, GetZip.asp
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Pagina senza titolo</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick()
{
document.getElementById("p_percorso").value = document.getElementById("Text1").value;
document.getElementById("form1").submit();
}
// ]]>
</script>
</head>
<body>
<form id="form1" method="get" action="getzip.aspx">
<span>Cartella da comprimere
</span>
<input id="Text1" type="text" style="width: 581px" />
<input id="Button1" type="button" value="Comprimi" onclick="return Button1_onclick()" />
<input id="p_percorso" name="p_percorso" type="hidden" />
</form>
</body>
</html>
sempre nella directory prova metto la pagina aspx che fa il lavoro, getzip.aspx
codice:
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="ICSharpCode.SharpZipLib.Zip" %>
<%@ Import Namespace="ICSharpCode.SharpZipLib.Checksums" %>
<script runat="server">
const string ZipFilePath = @"c:\tmp\foto.zip";
string OriginalFolderPath = "";
protected void Page_Load(object sender, EventArgs e)
{
string p_percorso = this.Request.QueryString["p_percorso"];
//if (string.IsNullOrEmpty(p_percorso)) return;
if (p_percorso == null || p_percorso == string.Empty) return;
OriginalFolderPath = p_percorso;
try
{
CreaFileZip();
SpedisciFile(ZipFilePath);
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
}
}
private void CreaFileZip()
{
Zip.ZipFolder(ZipFilePath, OriginalFolderPath);
}
private void SpedisciFile(string filePath)
{
Response.Expires = -1;
//' verifica esistenza
FileInfo fi = new FileInfo(filePath);
if (fi.Exists)
{
//' imposta le headers
Response.Clear();
Response.AddHeader(@"Content-Disposition", @"attachment; filename=""" + fi.Name + @"""");
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
//' leggo dal file e scrivo nello stream di risposta
Response.WriteFile(filePath);
Response.End();
}
else
Response.Write("Impossibile scaricare il file.");
}
//---------------------------------
//http://www.guru4.net/articoli/zip/
//---------------------------------
/// <summary>
/// Zip
/// </summary>
public class Zip
{
#region public methods
/// <summary>
/// Comprime il contenuto di un file in formato ZIP
/// </summary>
/// <param name="ZipFilePath">Path del file compresso da creare</param>
/// <param name="OriginalFilePath">Path del file da comprimere</param>
public static void ZipFile(string ZipFilePath, string OriginalFilePath)
{
ZipOutputStream zip = null;
try
{
FileInfo fi = new FileInfo(OriginalFilePath);
zip = new ZipOutputStream(File.Create(ZipFilePath));
zip.SetLevel(6); // 0 - store only to 9 - means best compression
AddFile2Zip(zip, fi, "");
zip.Finish();
}
catch (Exception ex)
{
throw ex;
}
finally
{ if (zip != null) zip.Close(); }
}
/// <summary>
/// Comprime il contenuto di una cartella in formato ZIP, ricorsivamente e preservandone la struttura
/// </summary>
/// <param name="ZipFilePath">Path del file compresso da creare</param>
/// <param name="OriginalFolderPath">Path della cartella da comprimere</param>
public static void ZipFolder(string ZipFilePath, string OriginalFolderPath)
{
ZipOutputStream zip = null;
try
{
DirectoryInfo di = new DirectoryInfo(OriginalFolderPath);
zip = new ZipOutputStream(File.Create(ZipFilePath));
zip.SetLevel(6); // 0 - store only to 9 - means best compression
AddFolder2Zip(zip, di, "");
zip.Finish();
}
catch (Exception ex)
{
throw ex;
}
finally
{ if (zip != null) zip.Close(); }
}
/// <summary>
/// Decomprime un file compresso ZIP nella cartella di destinazione specificata
/// </summary>
/// <param name="ZipFilePath">Path del file ZIP da decomprimere</param>
/// <param name="DestinationPath">Cartella di destinazione dell'archivio decompresso</param>
public static void UnZip(string ZipFilePath, string DestinationPath)
{
string dp = "";
ZipInputStream zip = null;
ZipEntry entry = null;
FileStream streamWriter = null;
try
{
if( !Directory.Exists(DestinationPath) ) Directory.CreateDirectory(DestinationPath);
dp = (DestinationPath.EndsWith("\\")) ? DestinationPath : DestinationPath + @"\";
zip = new ZipInputStream(File.OpenRead(ZipFilePath));
while ((entry = zip.GetNextEntry()) != null)
{
if (entry.IsDirectory)
Directory.CreateDirectory(dp + entry.Name);
else
{
streamWriter = File.Create(dp + entry.Name);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zip.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
//zip.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (streamWriter != null) streamWriter.Close();
if (zip != null) zip.Close();
}
}
#endregion
#region private methods
private static void AddFolder2Zip(ZipOutputStream zip, DirectoryInfo di, string internalzippath)
{
string izp = internalzippath + di.Name + "/"; // A directory is determined by an entry name with a trailing slash "/"
Crc32 crc = new Crc32();
ZipEntry entry = new ZipEntry(izp);
entry.Crc = crc.Value;
zip.PutNextEntry(entry);
foreach (FileInfo fi in di.GetFiles())
AddFile2Zip(zip, fi, izp);
foreach (DirectoryInfo sdi in di.GetDirectories())
AddFolder2Zip(zip, sdi, izp);
}
private static void AddFile2Zip(ZipOutputStream zip, FileInfo fi, string internalzippath)
{
Crc32 crc = new Crc32();
FileStream fs = File.OpenRead(fi.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(internalzippath + fi.Name);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zip.PutNextEntry(entry);
zip.Write(buffer, 0, buffer.Length);
}
#endregion
}
</script>
a te interessa la prima parte. Il file zippato viene archiviato in
c:\tmp\foto.zip
c:\tmp o la directory che vuoi tu, deve avere i permessi di scrittura per l'utente asp.net.
La pagina viene richiamata inviando il parametro get, p_percorso, che contiene il percorso da zippare.
Adesso si tratta solo di provare e riprovare: buona fortuna!