如何使用C#和J#将图像文件下载为Zip文件

目录

介绍

背景


介绍

最近,我不得不建立一个俱乐部网站,用户可以在其中下载图像作为压缩文件。因此,我计划制定一个程序,自动将图像文件转换为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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值