| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Java ファイルの分割結合

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Java ファイルの分割結合

Java | java sdk 5.0 以上

簡易ファイル分割結合ツール。JavaSDKがインストールされたPCでコンパイルして使ってください。

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FilenameFilter;
  8. import java.util.Arrays;
  9.  
  10. public class Split {
  11. private final int BUF_SIZE_1KB = 1024; //1kb
  12. private final String SEQ_LEN = "3";
  13. private final String FILE_SEQ_SUFFIX = "%0"+ SEQ_LEN + "d";
  14. private final String FILE_NAME_REP = "_[0-9]{"+ SEQ_LEN + "}$";
  15. public static void main(String[] args) {
  16. Split me = new Split();
  17. int splitSize = 1000; // 1mb default
  18. try {
  19. String mode = args[0];
  20. if ("-help".equals(mode)) {
  21. Split.printUsage();
  22. return;
  23. } else if
  24. ("s".equals(mode) || "-s".equals(mode)) {
  25. try {
  26. splitSize = Integer.parseInt(args[3]);
  27. } catch (Exception e) {}
  28. me.split(args[1], args[2], splitSize);
  29. } else if
  30. ("c".equals(mode) || "-c".equals(mode)) {
  31. me.concat(args[1]);
  32. } else {
  33. Split.printUsage();
  34. }
  35. } catch (Exception e) {
  36. Split.printUsage();
  37. e.printStackTrace();
  38. }
  39. System.out.println("finish");
  40. }
  41. /**
  42. * ファイルを指定サイズ(KB)で分割
  43. * @param inFile 対象ファイル
  44. * @param outDir 出力ディレクトリ
  45. * @param sizeKb サイズ
  46. * @throws Exception
  47. */
  48. public void split(String inFile, String outDir, int sizeKb) throws Exception {
  49. File od = createDir(outDir);
  50. byte[] buf = new byte[BUF_SIZE_1KB];
  51. BufferedInputStream is = getBufferedInputStream(inFile);
  52. BufferedOutputStream os = null;
  53. int sizeLimit = sizeKb;
  54. int sizeCnt = 0;
  55. int idx = 1;
  56. int ret = -1;
  57. boolean isFirstCreateFlg = true;
  58. while((ret = is.read(buf)) > 0) {
  59. if ((sizeCnt >= sizeLimit) || isFirstCreateFlg) {
  60. closeBufferdOutputStream(os);
  61. os = getBufferedOutputStream(
  62. absolutePath(od,
  63. (new File(inFile)).getName() + "_" + String.format(FILE_SEQ_SUFFIX, idx++))
  64. );
  65. isFirstCreateFlg = false;
  66. sizeCnt = 0;
  67. }
  68. os.write(buf,0, ret);
  69. os.flush();
  70. sizeCnt++; // kb
  71. }
  72. closeBufferdOutputStream(os);
  73. closeBufferdInputStream(is);
  74. }
  75. /**
  76. * ファイルを結合
  77. * @param outDir 分割されたファイルがあるディレクトリ
  78. * @throws Exception
  79. */
  80. public void concat(String outDir) throws Exception {
  81. File od = new File(outDir);
  82. if (!od.exists() || !od.isDirectory()) {
  83. return;
  84. }
  85. String[] fs = od.list(new FilenameFilter(){
  86. public boolean accept(File dir, String name) {
  87. return name.matches("^.*" + FILE_NAME_REP);
  88. }
  89. });
  90. BufferedInputStream is = null;
  91. BufferedOutputStream os = null;
  92. Arrays.sort(fs);
  93. byte[] buf = new byte[BUF_SIZE_1KB];
  94. int ret = -1;
  95. for (int i=0; i<fs.length ;i++) {
  96. if (os == null) {
  97. String originalFileName = absolutePath(od, fs[i].replaceFirst(FILE_NAME_REP, ""));
  98. os = getBufferedOutputStream(originalFileName);
  99. }
  100. is = getBufferedInputStream(absolutePath(od, fs[i]));
  101. while((ret = is.read(buf)) > 0) {
  102. os.write(buf, 0, ret);
  103. os.flush();
  104. }
  105. os.flush();
  106. closeBufferdInputStream(is);
  107. }
  108. closeBufferdOutputStream(os);
  109. }
  110.  
  111. private String absolutePath(File dir, String filename) {
  112. return dir.getAbsolutePath() + File.separator + filename;
  113. }
  114. private BufferedInputStream getBufferedInputStream(String inputFilename) throws FileNotFoundException {
  115. return new BufferedInputStream(new FileInputStream(new File(inputFilename)));
  116. }
  117. private void closeBufferdOutputStream(BufferedOutputStream os) throws Exception {
  118. if (os != null) {
  119. os.flush();
  120. os.close();
  121. }
  122. }
  123. private void closeBufferdInputStream(BufferedInputStream is) throws Exception {
  124. if (is != null) {
  125. is.close();
  126. }
  127. }
  128. private BufferedOutputStream getBufferedOutputStream(String fineName) throws Exception {
  129. File f = new File(fineName);
  130. f.createNewFile();
  131. return new BufferedOutputStream(new FileOutputStream(f));
  132. }
  133. private File createDir(String dirName) {
  134. File od = new File(dirName);
  135. if (!od.exists()) {
  136. od.mkdir();
  137. }
  138. return od;
  139. }
  140. private static void printUsage() {
  141. StringBuilder buf = new StringBuilder();
  142. buf.append("java split mode target_file output_dir [output_filesize(kb)]\n");
  143. buf.append("\tmode:-s split file, -c concatinate files\n");
  144. buf.append("\ttarget_file:target file to split.\n");
  145. buf.append("\toutput_dir:target directory for output splitted files.\n");
  146. buf.append("\toutput_filesize(kb):output file size as KB.\n");
  147. System.out.println(buf.toString());
  148. }
  149. }