前言

  最近碰到这样一个需求,用户需要批量打包下载sharepoint文档库中的文档,所以,就需要开发一个打包下载的服务。

  然后,把打包的代码分享给大家,也许会有需要的人。

        static void Main(string[] args)
{
string filesPath = "/DC/T1.txt;/DC/T2.txt";
CreateZipFile(filesPath, @"C:\Temp\", "http://localhost");
} /// <summary>
/// 创建压缩包
/// </summary>
/// <param name="filesPath"></param>
/// <param name="zipFilePath"></param>
private static void CreateZipFile(string filesPath, string zipFilePath, string siteUrl)
{
string[] filenames = filesPath.Split(';');
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
string tempName = Guid.NewGuid().ToString().Replace("-", "") + ".zip";
zipFilePath = zipFilePath + tempName;
Stream sr = File.Create(zipFilePath);
using (ZipOutputStream s = new ZipOutputStream(sr))
{
s.SetLevel();// 压缩级别 0-9
byte[] buffer = null;
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
string filename = web.Url + file;
SPFile ff = web.GetFile(filename);
buffer = ff.OpenBinary();
int sourceBytes = buffer.Length;
s.Write(buffer, , sourceBytes);
}
s.Finish();
s.Close();
sr.Close();
FileStream fs = File.Open(zipFilePath, FileMode.OpenOrCreate);
web.Lists["Temp"].RootFolder.Files.Add(web.ServerRelativeUrl + web.Lists["Temp"].RootFolder.Url + "/" + tempName, fs, true);
fs.Close();
File.Delete(zipFilePath);
}
}
}
return;
} public static string GetFileName(string filePath)
{
string fileName = filePath.Substring(filePath.LastIndexOf('/') + , filePath.Length - filePath.LastIndexOf('/') - );
return fileName;
}

SharePoint 压缩打包文件代码分享的更多相关文章

  1. java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹

    一.简述 zip用于压缩和解压文件.使用到的类有:ZipEntry  ZipOutputStream 二.具体实现代码 package com.joyplus.test; import java.io ...

  2. [转]C#压缩打包文件

    /// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass { /// <summary> /// ...

  3. asp.net C#压缩打包文件例子

    /// <summary> /// 压缩和解压文件 /// </summary> public class ZipClass { /// <summary> /// ...

  4. C#压缩打包文件

    该控件是使用csharp写的,因此可以直接在dotnet环境中引用,不需要注册. 利用 SharpZipLib方便地压缩和解压缩文件最新版本的SharpZipLib(0.84)增加了很多新的功能,其中 ...

  5. java.util.zip压缩打包文件总结二: ZIP解压技术

    一.简述 解压技术和压缩技术正好相反,解压技术要用到的类:由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInputStream设置冗余校验码,如: Checked ...

  6. Linux系统下压缩文件时过滤指定的文件 |Linux系统压缩指定文件代码

    进入要压缩的目录: [root@iZ25c748tjqZ wechat]# cd /alidata1/htdocs/wechat/ 查看目录: [root@iZ25c748tjqZ wechat]# ...

  7. C#强力粉碎文件代码分享,升级中用到

    360的文件粉碎机还是很强大的,在我们客户端winform升级的时候,必须将有些文件进行强力删除后下载更新,如果删除失败很有可能整个 程序就无法更新到最新的版本,所以这里参考了网上的资料整理了一个文件 ...

  8. linux文件压缩与文件夹压缩(打包)

    目录 一:linux文件压缩 1.linux常见的压缩包有哪些? 2.bzip压缩(文件) 二:打包(文件夹压缩) 1.打包命令 2.参数 3.参数解析(实战) 4.注意事项 简介: win中的压缩包 ...

  9. C# 压缩打包文件下载

    C# 压缩打包文件下载 public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform { #region INam ...

随机推荐

  1. ansible源码解读

    Ansible源码获取 Ansible Github:https://github.com/ansible Ansible目录结构 $ tree -L 2 ansible-2.0.0.0 ansibl ...

  2. 《MySQL技术内幕 InnoDB存储引擎 》学习笔记

    第1章  MySQL体系结构和存储引擎 1.3 MySQL存储引擎 数据库和文件系统最大的区别在于:数据库是支持事务的 InnoDB存储引擎: MySQL5.5.8之后默认的存储引擎,主要面向OLTP ...

  3. 【AtCoder】ARC101题解

    C - Candles 题解 点燃的一定是连续的一段,枚举左端点即可 代码 #include <bits/stdc++.h> #define enter putchar('\n') #de ...

  4. Codeforces 584E Anton and Ira

    Anton and Ira 我们把点分为三类, 向左走的, 向右走的, 不动的. 最完美的情况就是每个点没有走反方向. 每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优. #inclu ...

  5. 探究 encode 和 decode 的使用问题(Python)

    很多时候在写Python程序的时候都要在头部添加这样一行代码 #coding: utf-8 或者是这样 # -*- coding:utf-8 -*- 等等 这行代码的意思就是设定同一编码格式为utf- ...

  6. 高能天气——团队Scrum冲刺阶段-Day 6

    高能天气--团队Scrum冲刺阶段-Day 6 今日完成任务 于欣月:完善计步器功能:实现了历史步数统计和设置锻炼计划功能,并实现可视化图形界面 余坤澎:将闹钟部分和小游戏部分进行了合并 康皓越:配合 ...

  7. 我希望我知道的七个JavaScript技巧

    如果你是一个JavaScript新手或仅仅最近才在你的开发工作中接触它,你可能感到沮丧.所有的语言都有自己的怪癖(quirks)——但从基于强类型的服务器端语言转移过来的开发人员可能会感到困惑.我就曾 ...

  8. Codeforces Round #371 (Div. 2) B. Filya and Homework 水题

    B. Filya and Homework 题目连接: http://codeforces.com/contest/714/problem/B Description Today, hedgehog ...

  9. Chrome中使用老的标题栏界面

    Chrome 69中启用了新的UI界面,看着更加秀气了. 但新UI一个不好用的地方是标签栏太高了,留给windows标题栏的空白太小,导致拖动窗口位置非常不方便,如下是一个解决方法: 在地址栏输入: ...

  10. VIM简单配置(windows)

    set number set history=1000000 set tabstop=4 set shiftwidth=4 set smarttab set nocp filetype plugin ...