It is required to split the file (usually an archive) into a certain number of parts. I found a ready working version on c #, but could not adapt it to java. Please help translate the code or suggest a solution, thanks.
private void SplitFile(string FileInputPath, string FolderOutputPath, int OutputFiles) { // Store the file in a byte array Byte[] byteSource = System.IO.File.ReadAllBytes(FileInputPath); // Get file info FileInfo fiSource = new FileInfo(txtSourceFile.Text); // Calculate the size of each part int partSize = (int)Math.Ceiling((double)(fiSource.Length / OutputFiles)); // The offset at which to start reading from the source file int fileOffset = 0; // Stores the name of each file part string currPartPath; // The file stream that will hold each file part FileStream fsPart; // Stores the remaining byte length to write to other files int sizeRemaining = (int)fiSource.Length; // Loop through as many times we need to create the partial files for (int i = 0; i < OutputFiles; i++) { // Store the path of the new part currPartPath = FolderOutputPath + "\\" + fiSource.Name + "." + String.Format(@"{0:D4}", i) + ".part"; // A filestream for the path if (!File.Exists(currPartPath)) { fsPart = new FileStream(currPartPath, FileMode.CreateNew); // Calculate the remaining size of the whole file sizeRemaining = (int)fiSource.Length - (i * partSize); // The size of the last part file might differ because a file doesn't always split equally if (sizeRemaining < partSize) { partSize = sizeRemaining; } fsPart.Write(byteSource, fileOffset, partSize); fsPart.Close(); fileOffset += partSize; } } } private void JoinFiles(string FolderInputPath, string FileOutputPath) { DirectoryInfo diSource = new DirectoryInfo(FolderInputPath); FileStream fsSource = new FileStream(FileOutputPath, FileMode.Append); foreach (FileInfo fiPart in diSource.GetFiles(@"*.part")) { Byte[] bytePart = System.IO.File.ReadAllBytes(fiPart.FullName); fsSource.Write(bytePart, 0, bytePart.Length); } fsSource.Close(); }
usingconstruct is not used - if an exception is thrown, the files will remain unclosed. (Well, from somewhere, an incomprehensibletxtSourceFile.Text.) - VladD