Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- class SlicingFile
- {
- static void Main()
- {
- Slice("hubble.jpg", @"\..", 10);
- }
- static void Slice(string sourceFile, string destinationDirectory, int parts)
- {
- byte[] buffer = new byte[4096];
- int indexRead = 0;
- long fileSize = new FileInfo(sourceFile).Length;
- string extension = new FileInfo(sourceFile).Extension;
- int pieceSize = (int)Math.Ceiling((decimal)fileSize / parts);
- using (FileStream reader = new FileStream(sourceFile, FileMode.Open))
- {
- string destinationFileName = "Part-1" + extension;
- using (FileStream writer = new FileStream(destinationFileName, FileMode.Append))
- {
- for (int part = 0; part < parts; part++)
- {
- int indexWrite = 0;
- long currentPieceSize = 0;
- while (currentPieceSize < pieceSize)
- {
- // read piece
- int bytesRead = reader.Read(buffer, indexRead, buffer.Length);
- if (bytesRead == 0) break;
- // write piece
- writer.Write(buffer, indexWrite, bytesRead);
- indexRead += bytesRead;
- indexWrite += bytesRead;
- currentPieceSize += bytesRead;
- }
- destinationFileName = string.Format("Part-{0}", part + 2) + extension;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement