EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Open Zip File

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#26380
Posted: 09/10/2013 09:58:55
by Erkan Özkan (Basic support level)
Joined: 09/10/2013
Posts: 5

Hi, I am using SecureBlackBox to download file from Sftp. But file in ftp is zip file which has an Xml File. I want to download to a stream this xml file. How can i make this?

If It is not an zip file, I can download file to a stream like this.
Code
   public XmlDocument DownloadFile(string fileName)
            {
                using (ftpStream = new MemoryStream())
                {
                    XmlDocument doc = new XmlDocument();
                    ArrayList directoryList = new ArrayList();
                    SftpClient.ListDirectory("/", directoryList);
                    for (int i = 0; i < directoryList.Count; i++)
                    {
                        TElSftpFileInfo ts = new TElSftpFileInfo();
                        ts = (TElSftpFileInfo)directoryList[0];
                        if (ts.Name.Contains(fileName))
                        {
                            SftpClient.DownloadStream(ts.Name, ftpStream, TSBFileTransferMode.ftmAppendToEnd);
                        }
                    }
                    byte[] buffer = new byte[ftpStream.Length];
                    buffer = ftpStream.GetBuffer();
                    
                    string xmlString;
                    if (ftpStream.Length > 0)
                    {
                        xmlString = System.Text.UTF8Encoding.UTF8.GetString(buffer);
                        doc.LoadXml(xmlString);
                    }
                    return doc;
                }
            }
#26381
Posted: 09/10/2013 10:10:52
by Eugene Mayevski (EldoS Corp.)

Once you have a stream with data (ftpStream), you can use TElZIPReader class and its Open method that accepts a stream as a parameter. Then use that instance of TElZIPReader to access contents of the ZIP.

On a side note, when posting code pieces, please mark them with CODE tag or use CODE button (both opening and closing tags must be set).


Sincerely yours
Eugene Mayevski
#26384
Posted: 09/11/2013 02:03:04
by Erkan Özkan (Basic support level)
Joined: 09/10/2013
Posts: 5

I think, I opened zip file, but how can i download xml file? Can you send me an code example.

Code
TElZipReader zipReader = new TElZipReader();
zipReader.Open(ftpStream);
#26385
Posted: 09/11/2013 02:08:35
by Erkan Özkan (Basic support level)
Joined: 09/10/2013
Posts: 5

Ok, I download to local path.

Code
   zipReader.Extract("5006718102.xml", "c:/");


Now, How can i download xml file to a stream, not to a local path?
#26386
Posted: 09/11/2013 02:10:47
by Vsevolod Ievgiienko (EldoS Corp.)

Hello.

The code will look like this:

Code
zipReader.Directory.Entries[0].ExtractionStream := YourStreamForXMLFile;
zipReader.Extract(zipReader.Directory.Entries[0], nil);
#26387
Posted: 09/11/2013 02:56:35
by Erkan Özkan (Basic support level)
Joined: 09/10/2013
Posts: 5

I am getting error, "No output stream".

Code
MemoryStream xmlStream = new MemoryStream();
zipReader.Directory.get_Entries(0).ExtractionStream = xmlStream;
zipReader.Extract(zipReader.Directory.get_Entries(0), null);
#26388
Posted: 09/11/2013 03:15:16
by Vsevolod Ievgiienko (EldoS Corp.)

Then try to implement TElZipReader.OnExtractionStreamNeeded event handler and create an output stream inside it. Then simply call

Code
zipReader.Extract(zipReader.Directory.get_Entries(0), null);


Meanwhile I'll check why the first variant doesn't work.
#26389
Posted: 09/11/2013 03:44:10
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Actually, the method suggested by Vsevolod should work.
Please check which entry is the first in archive, probably it is a directory.
#26390
Posted: 09/11/2013 04:14:14
by Erkan Özkan (Basic support level)
Joined: 09/10/2013
Posts: 5

Hi, It's working. I forget a code block, and this causes to getting error.

Code
xmlStream = (MemoryStream)zipReader.Directory.get_Entries(0).ExtractionStream;


Thanks for your answers.
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 976 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!