Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Linking FTP Componant to the Zip Compant

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19498 |

    As I am recieving a file from the FTP componant, I would like to open up any .Zip files and unzip them prior to hitting the file system.

    I seem to be missing something to get the Xceed StreamFile and the System.IO.MemoryStream working together.

    Here is a snip of my code:

    ‘Get Zip or Normal file
    If GetFileExtension(TranslatedFileName).ToLower = “.zip” Then
    ‘Create a memory stream for the FTP Componant
    Dim MyMemoryStream As New System.IO.MemoryStream

    ‘Add an Abstract Xceed StreamFile object around the memory stream.
    Dim MyStreamFile As New Xceed.FileSystem.StreamFile(MyMemoryStream)

    ‘Recieve the file from the FTP componant into the memory stream.
    MyFtp.ReceiveFile(DirectoryEntries.Item(myloop).Name, MyMemoryStream)

    ‘At this point, the Memory stream shows it has a length ~ to the size of the FTPfile
    ‘At this point, the Streamfile shows a length of “zero”.

    Imported from legacy forums. Posted by gregory_may (had 7548 views)

    User (Old forums)
    Member
    Post count: 23064

    First, did you know the latest version of Xceed FTP for .NET now exposes the FtpFile and FtpFolder classes, which derive from AbstractFile and AbstractFolder? Thus, you can simply pass the required FtpFile to your ZipArchive constructor, so you can <a href=”http://blogs.xceedsoft.com/plantem/PermaLink,guid,78f813ed-96ee-4752-8ab5-2061b0f9d758.aspx”>read a zip file <b>directly</b> from the FTP server</a>?

    Second, if this does not suit you well and want to stick with using a memory stream, it is useless to create a MemoryStream and wrap it in a StreamFile. Just use a MemoryFile! Something like this:

    Dim MyMemoryFile As New MemoryFile
    Dim MyMemoryStream As Stream = MyMemoryFile.OpenRead()

    Then, you can pass this MemoryFile directly to your ZipArchive’s constructor.

    Imported from legacy forums. Posted by Martin (had 275 views)

    User (Old forums)
    Member
    Post count: 23064

    Ok, this is what I tried … “Exactly”:

    Dim MyMemoryFile As New Xceed.FileSystem.MemoryFile
    Dim MyMemoryStream As System.IO.Stream = MyMemoryFile.OpenRead

    When I try the MyMemoryFile.OpenRead, I get a message

    “Cannot perform the requested operation on an item that does not exist.”

    So, I tried this … The FTP works, but I get the same error on the MyMemoryFile.OpenRead:

    If GetFileExtension(TranslatedFileName).ToLower = “.zip” Then
    Dim MyMemoryFile As New Xceed.FileSystem.MemoryFile
    ‘Create a memory stream for the FTP Componant
    Dim MyMemoryStream As System.IO.Stream = New System.IO.MemoryStream

    ‘Recieve the file from the FTP componant into the memory stream.
    MyFtp.ReceiveFile(DirectoryEntries.Item(myloop).Name, MyMemoryStream)

    ‘Try to convert the memory stream to an Abstract MemoryFile (Fails).
    MyMemoryStream = MyMemoryFile.OpenRead

    Imported from legacy forums. Posted by gregory_may (had 239 views)

    User (Old forums)
    Member
    Post count: 23064

    Oups, sorry! I forgot to mention a “new MemoryFile” is a file that does not exist by default. Simply add “MyMemoryFile.Create()” between the two initial “Dim” statements.

    But again, may I ask why you’re not taking advantage of FtpFile instances instead? I’ve put a <a href=”http://blogs.xceedsoft.com/plantem/PermaLink,guid,af20b143-cb89-4b4e-8006-298cdff4c0ce.aspx”>new blog entry about it</a>.

    Imported from legacy forums. Posted by Martin (had 233 views)

    User (Old forums)
    Member
    Post count: 23064

    My first attempt at getting things working was based on the samples distributed with the http://FTP.Net componant. Later, I did notice your new filesystem work, but didnt see any clear benefit at first.

    So, I have reworked my code to take advantage of the new Abstract Filesystem. Currently this approach seems to be mostly working.

    Under the Abstract filesystem, I seem to have lost all access to events. I have a UI that was showing FTP progress and gives the user the ability to “stop” any “in process” transfers. I cant figure out how to rewire these events back into the UI under the Abstract method.

    Specifically these events seem to be unavailable from the GUI:

    .Abort()
    .Disconnect()

    And this event from within the FTP transfer:

    .FileTransferStatus

    The abstract filesystem seems to work better at giving me access to the files as they are FTP’d, but I lost the event Hooks so my UI can work right. I will post the “new way” thats working here. The code is a bit shorter, but loosing the “events” is a big deal:

    If GetFileExtension(TranslatedFileName).ToLower = “.zip” Then

    Dim MyZipArchive As New Xceed.Zip.ZipArchive(MyAbstractFile)
    MyZipArchive.TempFolder = New Xceed.FileSystem.MemoryFolder
    Dim MyZippedFile As Xceed.Zip.ZippedFile

    App_Print(” Extracting Zip to: ” & MyFtpSites(CurrentFTPIndex).DestinationUNC)
    For Each MyZippedFile In MyZipArchive.GetItems(True)
    Dim ZipTranslatedFileName As String = MyFtpSites(CurrentFTPIndex).TranslateFileName(MyZippedFile.Name)
    ‘Dim DestinationFile As Xceed.FileSystem.AbstractFile
    MyZippedFile.CopyTo(New Xceed.FileSystem.DiskFile(MyFtpSites(CurrentFTPIndex).DestinationUNC & “\” & ZipTranslatedFileName), True)
    App_Print(” Zip Extract: ” & MyZippedFile.Name & ” -> ” & ZipTranslatedFileName)
    Next

    Else
    MyFtp.ReceiveFile(MyAbstractFile.Name, MyFtpSites(CurrentFTPIndex).DestinationUNC & “\” & TranslatedFileName)
    End If

    Is looking like I either need access to the “events”, or I need to figure out how to get the Xceed.Ftp.FtpClient to work.

    Here is the current take on the Xceed.Ftp.FtpClient (old way). Under the old way (with your help), I can now create a .ziparchive via a MemoryFile … but it never has anything in it:

    If GetFileExtension(TranslatedFileName).ToLower = “.zip” Then
    ‘Create a memory stream for the FTP Componant
    Dim MyMemoryFile As New Xceed.FileSystem.MemoryFile
    Dim MyMemoryStream As System.IO.Stream = New System.IO.MemoryStream
    MyMemoryFile.Create()
    MyMemoryStream = MyMemoryFile.OpenWrite(True)

    ‘Recieve the file from the FTP componant into the memory stream.
    MyFtp.ReceiveFile(DirectoryEntries.Item(myloop).Name, MyMemoryStream)

    Dim MyZipArchive As New Xceed.Zip.ZipArchive(MyMemoryFile)
    Dim MyZippedFile As Xceed.Zip.ZippedFile

    For Each MyZippedFile In MyZipArchive.GetItems(True)
    Dim ZipTranslatedFileName As String = MyFtpSites(CurrentFTPIndex).TranslateFileName(MyZippedFile.Name)
    Dim DestinationFile As Xceed.FileSystem.AbstractFile
    MyZippedFile.CopyTo(New Xceed.FileSystem.DiskFile(MyFtpSites(CurrentFTPIndex).DestinationUNC & “\” & ZipTranslatedFileName), True)
    Next

    Else
    MyFtp.ReceiveFile(DirectoryEntries.Item(myloop).Name, MyFtpSites(CurrentFTPIndex).DestinationUNC & “\” & TranslatedFileName)
    End If

    Imported from legacy forums. Posted by gregory_may (had 280 views)

    User (Old forums)
    Member
    Post count: 23064

    Take a look at <a href=”http://blogs.xceedsoft.com/plantem/PermaLink.aspx?guid=b0d41310-6090-4db9-b828-86c8e17a20d7″>this blog post of mine</a>. It will explain how to handle events with the Xceed FileSystem.

    Imported from legacy forums. Posted by Martin (had 258 views)

    User (Old forums)
    Member
    Post count: 23064

    I Checked out your blog. Below is the bits that look like what I need. For some reason, I am not getting any fired events …… Did I miss a step for VB.Net?

    Procedure Snip ———
    Dim MyAbstractFile As Xceed.FileSystem.AbstractFile

    Dim MyFileSystemEvents As Xceed.FileSystem.FileSystemEvents = New Xceed.FileSystem.FileSystemEvents

    AddHandler MyFileSystemEvents.ItemProgression, AddressOf Me.ItemProgression
    AddHandler MyFileSystemEvents.ByteProgression, AddressOf Me.ByteProgression
    AddHandler MyFileSystemEvents.ScanningFolder, AddressOf Me.ScanningFolder

    For Each MyAbstractFile In MyFtpFolder.GetFiles(True, “*”)
    MyAbstractFile.CopyTo(New Xceed.FileSystem.DiskFile(“C:/temp.fil”, True)
    Next
    End Procedure Snip —-

    Private Sub ItemProgression(ByVal sender As Object, ByVal e As Xceed.FileSystem.ItemProgressionEventArgs)
    Try
    App_Print(“ItemProgression: ” & e.CurrentItem.FullName)

    Catch ex As Exception
    App_Print(ex.Message)
    End Try
    End Sub

    Private Sub ByteProgression(ByVal sender As Object, ByVal e As Xceed.FileSystem.ByteProgressionEventArgs)
    Try
    App_Print(“ByteProgression: ” & e.CurrentItem.FullName)
    Catch ex As Exception
    App_Print(ex.Message)
    End Try
    End Sub

    Private Sub ScanningFolder(ByVal sender As Object, ByVal e As Xceed.FileSystem.ScanningFolderEventArgs)
    Try
    App_Print(“ScanningFolder: ” & e.CurrentItem.FullName)
    Catch ex As Exception
    App_Print(ex.Message)
    End Try
    End Sub

    Imported from legacy forums. Posted by gregory_may (had 254 views)

    User (Old forums)
    Member
    Post count: 23064

    You need to pass “MyFileSystemEvents” to every method that accepts a “FileSystemEvents”. In your example, the GetFiles method can raise the ScanningFolder event, and the CopyTo method will generate both ItemProgression and ByteProgression.

    May I ask what is your goal in copying every file over the same “C:\temp.fil” local file? I guess it’s only a test? Because if you use MyFtpFolder.CopyFilesTo instead of calling GetFiles recursively, looping on each file to call CopyTo, your progression events will nicely go from 0 to 100% a single time, instead of doing 0-100% for each file.

    Imported from legacy forums. Posted by Martin (had 393 views)

    User (Old forums)
    Member
    Post count: 23064

    OK,

    I wasnt using the correct signature of the CopyTo function. It took me some looking to find something that takes FileSystemEvents as a Parm.

    Thanks for the tip on the CopyFilesTo method. I will give it a shot.

    So, It looks like its working well now. I just have to wire in an “Abort” button based on your other Blog postings.

    Thanks!

    Imported from legacy forums. Posted by gregory_may (had 270 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m creating a routine to copy any AbstractFile to an FTPFile…

    If the user decides they don’t want to copy a large file and click “Cancel”….how can I abort the copy operation using AbstractFile?

    I can see how to do it with the FTP control, but I can’t see how to do it with the Abstract CopyTo or CopyToFiles..?

    Any ideas?

    Imported from legacy forums. Posted by cguerin (had 235 views)

    Xceed Support
    Member
    Post count: 5658

    Hi,

    Martin Plante wrote an article on this. You can check it out at this link:

    http://blogs.xceedsoft.com/plantem/PermaLink.aspx?guid=cff8fc14-76a1-4fde-a4f0-a49ab582a21a

    Hope this helps!

    Imported from legacy forums. Posted by Jacques [Xceed] (had 7982 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.