/// ファイルを受信する public static void receiveFiles(Network network, String strDirectory, FolderLANSync.SyncFolderMaster parent) { if ( strDirectory.Length > 3 && !Directory.Exists(strDirectory)) Directory.CreateDirectory(strDirectory); while (true) { byte nFlag = (byte)network.receiveByte(); if (nFlag == 0) break; String strFilePath = network.receiveString(); parent.addLog("「" + strFilePath + "」を受信開始"); byte[] data = network.receiveBinary(); // 先に受信してしまう strFilePath = Path.Combine(strDirectory, strFilePath); parent.beginReceiveFile(strFilePath); //フォルダを変更する前に呼び出す.監視をオフにする。 #if true // スレッド同期とってないし封印 { ReceiveThread receiveThread = new ReceiveThread(data, parent, strFilePath); Thread thread = new Thread(new ThreadStart(receiveThread.receiveFileThread)); thread.Start(); } #else Directory.CreateDirectory(Path.GetDirectoryName(strFilePath)); System.IO.FileStream wout = null; for (int it = 0; it < 3; ++it) { try { wout = new System.IO.FileStream(strFilePath, FileMode.Create); } catch (Exception ex) { parent.addLog("「" + strFilePath + "」が上書きできませんでした"); System.Threading.Thread.Sleep(500); } if (wout != null) { if (it > 0) { parent.addLog("「" + strFilePath + "」に書き込めました"); } } } if (data != null) { System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(new MemoryStream(data), System.IO.Compression.CompressionMode.Decompress); byte[] buffer = new byte[1024 * 1024 * 50]; while (true) { int nReadSize = gzip.Read(buffer, 0, buffer.Length); if (nReadSize == 0) break; if ( wout != null ) wout.Write(buffer, 0, nReadSize); } gzip.Close(); } parent.addLog("「" + strFilePath + "」を受信終了"); if (wout != null) wout.Close(); parent.endReceiveFile(strFilePath); //フォルダを変更する前に呼び出す.監視をオフにする。 #endif } }
/// ファイルを送信する public static void sendFiles(Network network, String strDirectoryOrFile) { try { if (File.Exists(strDirectoryOrFile)) { sendFile(network, Path.GetFileName(strDirectoryOrFile), Path.GetFullPath(strDirectoryOrFile), ""); } else { sendFilesSub(network, strDirectoryOrFile, ""); } } finally { network.sendByte(0); } }
private static void sendFilesSub(Network network, String strDirectory, String strSubPath) { DirectoryInfo dirinfo = new DirectoryInfo(strDirectory); foreach (var file in dirinfo.GetFiles()) { sendFile(network, file.Name, file.FullName, strSubPath); } foreach (var dir in dirinfo.GetDirectories()) { sendFilesSub(network, dir.FullName, Path.Combine(strSubPath, dir.Name)); } }
// ファイル転送 strSubPathは付加するフォルダ名 public static void sendFile(Network network, String strFileName, String strFullPath, String strSubPath) { try { MemoryStream memory = new MemoryStream(); bool bWrite = false; using (System.IO.FileStream win = new System.IO.FileStream(strFullPath, FileMode.Open, FileAccess.Read)) { System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(memory, System.IO.Compression.CompressionMode.Compress); // System.IO.Compression.DeflateStream gzip = new System.IO.Compression.DeflateStream(memory, System.IO.Compression.CompressionMode.Compress); byte[] buffer = new byte[1024 * 1024 * 50]; while (true) { int nReadSize = win.Read(buffer, 0, buffer.Length); if (nReadSize == 0) break; bWrite = true; gzip.Write(buffer, 0, nReadSize); // memory.Write(buffer, 0, nReadSize); } gzip.Close(); } if (bWrite) { network.sendByte(1); String strTransferPath = Path.Combine(strSubPath, strFileName); network.sendString(strTransferPath); byte[] data = memory.ToArray(); network.sendBinary(data); } memory.Close(); } catch (Exception ex) { throw ex; // MessageBox.Show(ex.Message); } }