目录
介绍
最近,我不得不建立一个俱乐部网站,用户可以在其中下载图像作为压缩文件。因此,我计划制定一个程序,自动将图像文件转换为zip文件。在线搜索使我进入了一个博客,其中包含在J#中使用ZIP选项的绝妙想法,它真的吸引了我。
背景
在本文中,我将解释C#代码中ZIP功能在J#中的用法。此应用程序中的代码设计为以复制粘贴方式重用,而不是作为库。
此应用程序在内部使用J#类。为此,我们必须首先引用J# .NET库。从物理上讲,它以名为vjslib.dll的文件的形式驻留。如果您不太确定如何在项目中引用库,请按照以下步骤操作:
在服务器资源管理器中右键单击您的项目,然后单击“添加引用”->选择.NET选项卡->向下滚动并选择“vjslib”->单击“确定”,您就在那里。现在,您可以在应用程序中引用Java库类。
列表1——指令
using java.util;
using java.util.zip;
using java.io;
java.util.zip命名空间包含用于实现压缩和解压缩功能的类和方法。上述命名空间中使用的主要类是
- ZipFile
- ZipEntry
- ZipOutputSteam
- Enumeration
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using java.util;
using java.util.zip;
using java.io;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lnkImage_Click(object sender, EventArgs e)
{
string FilePath = Server.MapPath("~/Image/");
string FileDest = Server.MapPath("~/Image/");
Zip(FileDest + "test.zip", FilePath + "Download_File_As_Zip_File/pic.jpg");
}
private void Zip(string zipFileName, string sourceFile)
{
FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
FileInputStream filIpStrm = null;
filIpStrm = new FileInputStream(sourceFile);
ZipEntry ze = new ZipEntry(Path.GetFileName(sourceFile));
zipOpStrm.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len = 0;
while ((len = filIpStrm.read(buffer)) >= 0)
{
zipOpStrm.write(buffer, 0, len);
}
zipOpStrm.closeEntry();
filIpStrm.close();
zipOpStrm.close();
filOpStrm.close();
FileInfo file = new FileInfo(zipFileName);
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=" + "test.zip");
Response.WriteFile(file.FullName);
Response.End();
}
protected void lnkSource_Click(object sender, EventArgs e)
{
//Downloadzip.zip
FileInfo file = new FileInfo(Server.MapPath("~/Image/") +
"Downloadzip.zip");
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=" + "Downloadzip.zip");
Response.WriteFile(file.FullName);
Response.End();
}
}
https://www.codeproject.com/Tips/22902/How-to-download-an-image-file-as-a-Zip-file-using