zoukankan      html  css  js  c++  java
  • C#压缩加密和vb压缩加密

    string[] FileProperties = new string[2];
    FileProperties[0] = "C:\a\";//待压缩文件目录
    FileProperties[1] = "C:\zip\a.zip"; //压缩后的目标文件
    zipclass Zc = new zipclass();
    Zc.ZipFileMain(FileProperties,"123");

    压缩文件夹  加密

    public void ZipFileMain(string[] args, string password)
    {
    string[] filenames = Directory.GetFiles(args[0]);

    ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));

    s.SetLevel(6); // 0 - store only to 9 - means best compression

    s.Password = password;

    foreach (string file in filenames)
    {
    //打开压缩文件
    FileStream fs = File.OpenRead(file);

    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);

    Array arr = file.Split('\');
    string le = arr.GetValue(arr.Length - 1).ToString();
    ZipEntry entry = new ZipEntry(le);
    entry.DateTime = DateTime.Now;
    entry.Size = fs.Length;
    fs.Close();
    s.PutNextEntry(entry);
    s.Write(buffer, 0, buffer.Length);
    }
    s.Finish();
    s.Close();
    }
    }

    通过密码解压

       public void UnZip(string[] args, string password)
    {
    string directoryName = Path.GetDirectoryName(args[1]);
    using (FileStream fileStreamIn = new FileStream(args[0], FileMode.Open, FileAccess.Read))
    {
    using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))
    {
    zipInStream.Password = password;
    ZipEntry entry = zipInStream.GetNextEntry();
    do
    {
    using (FileStream fileStreamOut = new FileStream(directoryName + @"" + entry.Name, FileMode.Create, FileAccess.Write))
    {

    int size = 2048;
    byte[] buffer = new byte[2048];
    do
    {
    size = zipInStream.Read(buffer, 0, buffer.Length);
    fileStreamOut.Write(buffer, 0, size);
    } while (size > 0);
    }
    } while ((entry = zipInStream.GetNextEntry()) != null);
    }
    }
    }

    压缩某个文件
     public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)
    {
    if (!System.IO.File.Exists(FileToZip))
    {
    throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
    }

    System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
    ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
    ZipEntry ZipEntry = new ZipEntry("ZippedFile");
    ZipStream.PutNextEntry(ZipEntry);
    ZipStream.SetLevel(CompressionLevel);
    byte[] buffer = new byte[BlockSize];
    System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
    ZipStream.Write(buffer, 0, size);
    try
    {
    while (size < StreamToZip.Length)
    {
    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
    ZipStream.Write(buffer, 0, sizeRead);
    size += sizeRead;
    }
    }
    catch (System.Exception ex)
    {
    throw ex;
    }
    ZipStream.Finish();
    ZipStream.Close();
    StreamToZip.Close();
    }

    通过工具c#转vb得出基本代码http://converter.telerik.com/

    Imports ICSharpCode.SharpZipLib.Zip
    Imports System.IO
    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim FileProperties As String() = New String(1) {}
    FileProperties(0) = "C:a"
    '待压缩文件目录
    FileProperties(1) = "C:zipa.zip"
    '压缩后的目标文件
    ZipFileMain(FileProperties, "123")
    End Sub

    Private Function ZipFileMain(args As String(), password As String) As String

    Dim filenames As String() = Directory.GetFiles(args(0))
    Dim myTank As ZipOutputStream
    myTank = New ZipOutputStream(File.Create(args(1)))
    myTank.SetLevel(6)
    myTank.Password = password

    For Each file__1 As String In filenames
    '打开压缩文件
    Dim fs As FileStream = File.OpenRead(file__1)

    Dim buffer As Byte() = New Byte(fs.Length - 1) {}
    fs.Read(buffer, 0, buffer.Length)

    Dim arr As Array = file__1.Split(""c)
    Dim le As String = arr.GetValue(arr.Length - 1).ToString()
    Dim entry As New ZipEntry(le)
    entry.DateTime = DateTime.Now
    entry.Size = fs.Length
    fs.Close()
    myTank.PutNextEntry(entry)
    myTank.Write(buffer, 0, buffer.Length)

    Next
    myTank.Finish()
    myTank.Close()
    Return password
    End Function
    End Class

  • 相关阅读:
    重打技术征集系统第七稿开发第1、2天
    关于centos7右上角网络图标消失的解决办法
    记录一下hbase踩的坑
    在jsp中,单选按钮的点击事件(点击隐藏或显示输入框)
    input中禁止输入特殊字符
    《软件方法》阅读笔记——1
    随手快递app开发第十七天
    Codeforces Round #284 (Div. 1) B. Name That Tune(最大流)
    hihocoder 1310岛屿(dfs,一个搜索技巧)
    Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(lca+思维,树上寻找与给定两个点距离相等的点的个数)
  • 原文地址:https://www.cnblogs.com/sdya/p/5475574.html
Copyright © 2011-2022 走看看