public void MuliteImport()
{
string filePath = @"D:\Program Files\1.rar";//绝对路径
//解压目录
string virtualPath = string.Format("{0}/{1}", "Decompress", DateTime.Now.ToString("yyyyMMdd"));
string FileEextension = Path.GetExtension(filePath);//文件名后缀
//如果解压目录不存在则创建
if (!Directory.Exists(virtualPath))
{
Directory.CreateDirectory(virtualPath);
}
CatalogAndFile catalogAndFile = new CatalogAndFile();
catalogAndFile.listCatalogue = new List<Catalogue>();
catalogAndFile.listFileClass = new List<FileClass>();
catalogAndFile.listAnexesFile = new List<AnnexesFile>();
if (FileEextension.ToLower() == ".zip")
{
catalogAndFile = Decompress(virtualPath, filePath, catalogAndFile);
}
else if (FileEextension.ToLower() == ".rar")
{
catalogAndFile = DeCompressRar(virtualPath, filePath, catalogAndFile);
}
}
#region 解压缩文件
/// <summary>
/// 解压zip格式的文件
/// </summary>
/// <param name="unZipDir">解压文件存放路径</param>
/// <param name="zipFilePath">压缩文件路径</param>
/// <returns></returns>
public CatalogAndFile Decompress(string unZipDir, string zipFilePath, CatalogAndFile catalogAndFile)
{
UserInfo userInfo = LoginUserInfo.Get();
unZipDir = unZipDir + "/zip";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);//生成解压目录
byte[] data = new byte[2048];
int size = 2048;
//一个ZIP文件中的条目,可以理解为压缩包里面的一个文件夹/文件
ZipEntry theEntry = null;
string fileCatalogID = "";
string rootDir = "";//当前目录的上级目录路径
int index = 0;
List<CatalogIdAndName> listCatalogIdAndName = new List<CatalogIdAndName>();
//解压缩输出流,从压缩包中一个接一个的读出文档。
using (ZipInputStream inputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
while ((theEntry = inputStream.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 该结点是目录
if (!Directory.Exists(unZipDir + theEntry.Name))
{
Directory.CreateDirectory(unZipDir + "/" + theEntry.Name);
}
if (index != 0)
{
Catalogue catalogue = new Catalogue();
catalogue.CatalogueID = Guid.NewGuid().ToString();
catalogue.CatalogueName = theEntry.Name.TrimEnd('/').Substring(theEntry.Name.TrimEnd('/').LastIndexOf("/") + 1);
if (listCatalogIdAndName == null || listCatalogIdAndName.Count == 0)
{
catalogue.UpCatalogueID = "";
C#解压缩rar或zip,并把压缩包的目录名称、名称插入到数据库目录表、文件表
最新推荐文章于 2024-01-29 11:12:16 发布