File Transfer (1)-compressed files

Source: Internet
Author: User
Tags getstream msmq

There are many ways of interaction between heterogeneous systems. Ax's AIF framework provides the possibility of implementing various interaction methods through files, Web Services, and MSMQ, I always think that these methods are a bit itchy. As ax, it cannot provide corresponding interfaces for other non-mainstream heterogeneous systems. It can only set one interface standard, let other heterogeneous systems prepare data according to its format. for general purposes, it should adopt XML and other standard format files as much as possible. Other heterogeneous systems must prepare interface data according to its format, writing this interface is usually a pain point. I would rather understand the implementation of other systems than to let people in heterogeneous systems understand the meaning of each field of ax, interfaces of other systems in ax, and many heterogeneous systems do not have the ideal support for interfaces provided by ax, and efficiency is also a problem in the current network status, so I feel that AIF is a good stuff, but there are some good ideas. I hope one day I will encounter a project that will give me a chance to play with this stuff.

It seems that the question is gone...
I remember hearing from a friend that at the beginning of the 1990s S, data transmission between different segments was based on mail, and many companies are still using this method, although it is not very reliable and efficient, it is enough in most cases. Recently, a project needs to transmit data between the Branch Office and the headquarters, and the network is not very stable, the most basic requirement is that files must be reliably transmitted. resumable upload can be performed when the network is disconnected. When it comes to reliable file transmission, I first thought of MQ. I have read some introduction to MQ. Article , MQ is a good scheme for asynchronous file transmission, but IBM's MQ requires money. Microsoft's MSMQ took a day or two to find relevant documents on the Internet and want to develop a simple test item. It feels pretty good on the LAN and there is no problem, some inexplicable problems occurred when testing the file transfer over the Internet. I used Google to search for the network and did not find any words. The MSMQ information was too small, even if the problem was solved, I don't know how to solve other problems in the future. It's enough to make ax, which is something that few people are playing with, so I don't have to live if I try to provoke MSMQ to be played by fewer people, haha. So I gave up MQ and switched to FTP, which has a relatively large amount of data. Microsoft's IIS components include FTP, which is free of charge. Other people's names are called file transfer protocol, so I should use it.
To improve the efficiency and reliability of data transmission, you must:
1. compressed file
2. resumable upload
This document describes the implementation of compressed files.
There are many implementation methods for compressing files. Based on a lot of information on the Internet, there are applications that call WinRAR and other compression tools in. net. Program On the Internet, some people have written off-the-shelf classes. There is a blog that summarizes the method of compressing files into ZIP files. I will not go into details here. The link to this article is as follows:
Http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx
Finally, I used the related classes in the class library "system. Io. Packaging" added by. Net framework3.0 mentioned above. Code I already have it. I have posted it here and only made a few changes:

Code
Private   Void Preparefiletobeuploaded ()
{
If ( ! Localuploadaddress. endswith ( @" \ " ))
Localuploadaddress + =   @" \ " ;

System. Io. directoryinfo dirinfo= NewDirectoryinfo (localuploadaddress );
System. Io. fileinfo [] fileinfos=Dirinfo. getfiles ();

// Get files to be uploaded
Foreach (Fileinfo In Fileinfos)
{
// ZIP file
This . Zipfiles (localuploadaddress + Zipfilename +   " . Zip " , Fileinfo. fullname );
// Backup File
Fileinfo. copyto (localuploadbackupaddress + Fileinfo. Name );
// Delete file
Fileinfo. Delete ();

}
}

Private   Void Zipfiles ( String Zipfilename, String Filetoadd)
{
Using (Package zip = System. Io. Packaging. Package. Open (zipfilename, filemode. openorcreate ))
{
String Destfilename =   " .\\ "   + Path. getfilename (filetoadd );
Uri URI = Packurihelper. createparturi ( New Uri (destfilename, urikind. Relative ));
If (Zip. partexists (URI ))
{
Zip. deletepart (URI );
}
Packagepart part = Zip. createpart (Uri, "" , Compressionoption. Normal );
Using (Filestream =   New Filestream (filetoadd, filemode. Open, fileaccess. Read ))
{
Using (Stream dest = Part. getstream ())
{
Copystream (filestream, DEST );
}
}
}

}

The decompressed code is as follows:

Code
Private   Static   Void Decompression ( String Filename)
{
Zippackage zipfile;

Zipfile = Zippackage. Open (filename, filemode. open) As Zippackage;

Foreach (Zippackagepart part In Zipfile. getparts ())
{
Stream stream = Part. getstream ();
Long Buffersize = Stream. Length < Buffer_size ? Stream. Length: buffer_size;
String Filenamelocal;
Int Bytesread =   0 ;
Byte [] Buffer =   New   Byte [Buffersize];

Filenamelocal = Part. Uri. tostring (). substring ( 1 , Part. Uri. tostring (). Length -   1 );

Filestream FS =   New Filestream ( @" D :\ " + Filenamelocal, filemode. openorcreate );
While ( True )
{
Bytesread = Stream. Read (buffer, 0 , Buffer_size );
If (Bytesread =   0 )
Break ;
FS. Write (buffer, 0 , Bytesread );

}
FS. Close ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.