Riporto l'intero codice per la creazione dello ZIP.
Come già detto, funziona perfettamente solo se il percorso e nome del file da creare è scritto in questo modo.
codice:
const string ZipFilePath = @"D:\inetpub\webs\mywebit\public\ZipFile.zip";
Mentre il percorso della cartella da zippare riesco tranquillamente a passarla come parametro.
codice:
string p_percorso = Server.MapPath(this.Request.QueryString["p_percorso"]);
Devo assolutamente potergli passare anche dove creare il file e come chiamarlo.
GRAZIE a chi mi aiuta!
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 = @"D:\inetpub\webs\mywebit\public\ZipFile.zip";
string OriginalFolderPath = "";
protected void Page_Load(object sender, EventArgs e)
{
string p_percorso = Server.MapPath(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>