提取并阅读.net 4.5+中的zip文件 [英] Extract and Read a zip File in .net 4.5+

查看:109
本文介绍了提取并阅读.net 4.5+中的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个zip文件并将其解压缩并读取该文件的内容并逐行插入到sql db中。

I am trying to read a zip file and extract it and read the contents of the file and insert row by row into sql db.

zip文件内容正在巨大的文本文件,想用ADO.NET逐一阅读。

The zip file contents are having huge size text files and wanted to read it one by one with ADO.NET.

下面是我正在尝试的代码。

below is the code am trying.

但是我不能正确使用。

使用以下LOC:

var tempFileName = Path.GetTempFileName()

var tempFileName = Path.GetTempFileName()

这里,A 临时文件,.tmp扩展名已创建,我无法在处理后完成最终路径:

here,a  temp file , .tmp extension is created and i am unable to fine the final path after processing :

C:\ Users \muysername1 \ AppData \ Local \ Temp \ tmp8B51.tmp

C:\Users\muysername1\AppData\Local\Temp\tmp8B51.tmp

最后当代码执行时,我越来越感觉:路径无法找到! 

我做错了什么?我应该使用哪个API来提取和阅读?

what am I doing wrong? which API should i use to extract and read ?

任何人都可以提供帮助, 使用正确的源代码:

can anyone help,  with the correct source code :

 试试
            {

                pathforsaving = Server.MapPath(@"〜/ App_Data /");
$


                if(postedFiles!= null)

                {

                    string sfileName = Path.GetFileName(postingFiles.FileName);

                    string uploadFilePathandname = Path.Combine(pathforsaving,sfileName);



                    postedFiles.SaveAs(uploadFilePathandname);



                    //ZipFile.ExtractToDirectory(uploadFilePathandname,pathforsaving);



                    //读取zip文件中的文件而不提取 

                   使用(var zipStream = new FileStream(uploadFilePathandname,FileMode.Open))

                   使用(var archive = new ZipArchive(zipStream,ZipArchiveMode.Read))

                    {

                        foreach(archive.Entries中的var条目)

                        {

                            if(entry!= null&&!string.IsNullOrEmpty(entry.ToString()))

                            {

                               使用(var stream = entry.Open())

                                {

                                   使用(var streamReader = new StreamReader(uploadFilePathandname))

                                    {

              }

 try
            {
                pathforsaving = Server.MapPath(@"~/App_Data/");

                if (postedFiles != null)
                {
                    string sfileName = Path.GetFileName(postedFiles.FileName);
                    string uploadFilePathandname = Path.Combine(pathforsaving, sfileName);

                    postedFiles.SaveAs(uploadFilePathandname);

                    //ZipFile.ExtractToDirectory(uploadFilePathandname, pathforsaving);

                    //read files within the zip file without extracting 
                    using (var zipStream = new FileStream(uploadFilePathandname, FileMode.Open))
                    using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Read))
                    {
                        foreach (var entry in archive.Entries)
                        {
                            if (entry != null && !string.IsNullOrEmpty(entry.ToString()))
                            {
                                using (var stream = entry.Open())
                                {
                                    using (var streamReader = new StreamReader(uploadFilePathandname))
                                    {
              }

推荐答案

提取内容并将文件内容存储到数据库中是什么意思? zip文件可能包含零个或多个文件夹中包含的零个或多个文件。每个文件都可以是任何东西。请详细说明您要做的事情。

What do you mean by extract the contents and store the contents of the file into a database? A zip file may contain zero or more files contained in zero or more folders. Each file can be just about anything. Please clarify exactly what you're trying to do.

要提取zip文件,您可以使用ZipArchive或ZipFile。两者都可以让你访问zip中的文件。之后,您可以使用标准的File API来读取文件或执行任何操作。但是,您当然无法直接将文件上传到数据库中。
这没有意义。

To extract a zip file you can use ZipArchive or ZipFile. Both will give you access to the file(s) in the zip. After that you can use the standard File API to read the files or do whatever you want. But of course you cannot directly upload a file into a database. That wouldn't make sense.


这篇关于提取并阅读.net 4.5+中的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆