Pagina 1 di 1

split file

Inviato: ven 7 ott 2005, 19:21
da Teo
Ciao a tutti,
avrei bisogno di un programma open source per dividere un file di grosse dimensioni in files più piccoli. Avrei bisogno che esistesse anche (o solo) la versione per Windows. Ho provato a cercare qualcosa, ne ho trovati di freeware, ma non open source. Sapete aiutarmi?

Inviato: ven 7 ott 2005, 20:54
da masalapianta
man split
(credo tu possa farlo girare anche sotto windows con cygwin)

Inviato: ven 7 ott 2005, 21:19
da useless
tempo fa (in DOS) ho scritto questo, devo dire che funziona +ttosto bene. consideratelo GPL:

Codice: Seleziona tutto

/* SukkoSplit
   Note: The program will overwrite any existing files without confirmation.
   */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX_FILENAME 13
#define BUF_SIZE 1024
#define VERSION "0.7 (08/09/2001)"


void show_welcome (void);
void show_usage (char *prog_name);
long int get_file_size (char *filename);
int do_split (FILE *f_in, char *filename, long int file_size,
              long int part_size, int parts, int tracks);


int main (int argc, char *argv[]) {
  int out;
  char mode;

  out = EXIT_SUCCESS;

  show_welcome ();

  if (argc != 4 || ((mode = tolower (argv[1][1])) != 'p' && mode != 's' &&
                                                            mode != 'a')) {
    show_usage (argv[0]);
    out = EXIT_FAILURE;
  } else {
    FILE *f_in;
    char *filename;
    long int file_size;

    filename = argv[2];
    file_size = get_file_size (filename);

    if ((f_in = fopen (filename, "rb")) == NULL) {
      fprintf (stderr, "Cannot open file \"%s\".\n", filename);
      out = EXIT_FAILURE;
    } else {
      long int parts, part_size;
      int tracks;

      /* Start as non -a mode */
      tracks = 0;

      /* Select operating mode */
      if (mode == 'p') {
        parts = strtol (argv[3], NULL, 10);
        part_size = file_size / parts + (file_size % parts != 0);;

        printf ("File size is %ld bytes, so it will be splitted into %d bytes long"
                " parts.\n", file_size, part_size);
      } else if (mode == 's') {
        part_size = strtol (argv[3], NULL, 10);
        parts = file_size / part_size + (file_size % part_size != 0);

        printf ("File size is %ld bytes, so it will be splitted into %d parts.\n",
                file_size, parts);
      } else if (mode == 'a') {
        /* Extract tracks from an 'Amiga Disk File' (ADF) */
        long int b;

        parts = strtol (argv[3], NULL, 10);
        part_size = file_size / parts + (file_size % parts != 0);

        for (tracks = 0; (b = tracks * 512 * 11 * 2) < part_size; tracks++)
          ;

        printf ("File size is %ld bytes, so it will be splitted into %d"
                " parts, each %ld bytes long and containing %d tracks.\n",
                file_size, parts, b, tracks);

        part_size = b;
      }

      if (parts <= 1) {
        printf ("It's no use splitting a file in a single part ;).\n");
        out = EXIT_FAILURE;
      } else
        out = do_split (f_in, filename, file_size, part_size, parts, tracks);

      fclose (f_in);
    }
  }

  return (out);
}


void show_welcome (void) {
  fprintf (stderr, "*** SukkoSplit V %s\n"
                   "    by SukkoPera (enjoy.the.silence@iol.it) ***\n\n",
                   VERSION);

  return;
}


void show_usage (char *prog_name) {
  fprintf (stderr, "To split a file into parts of a given size:\n"
                   " -> %s -s <file> <size_in_bytes>\n\n"
                   "To split a file into a given number of parts:\n"
                   " -> %s -p <file> <number_of_parts>\n", prog_name,
                   prog_name);

  return;
}
    

long int get_file_size (char *filename) {
  struct stat ss;

  stat (filename, &ss);

  return ((long int) ss.st_size);
}


char *setup_part_name (char *filename, char *part_name, int part_no) {
  char temp[5];
  int i;

  for (i = 0; filename[i] != '\0' && filename[i] != '.'; i++)
    part_name[i] = filename[i];

  /* NULL-terminate */
  part_name [i] = '\0';

  sprintf (temp, ".%03d", part_no);
  strcat (part_name, temp);

  return (part_name);
}


int do_split (FILE *f_in, char *filename, long int file_size,
               long int part_size, int parts, int tracks) {
  int out;
  unsigned char *b;

  out = EXIT_SUCCESS;

  /* Memory for the file buffer is dynamically allocated */
  if ((b = (unsigned char *) malloc (sizeof (char) * BUF_SIZE)) == NULL) {
    fprintf (stderr, "Out of memory!\n");
    out = EXIT_FAILURE;
  } else {
    int cur_part;
    long int i, buf_length;
    size_t r;
    char part_name[MAX_FILENAME];
    FILE *f_out;

    cur_part = 0;
    buf_length = 0;

    while (++cur_part <= parts && out == EXIT_SUCCESS) {
      /* Set up the filename for the current part */
      setup_part_name (filename, part_name, cur_part);

      if ((f_out = fopen (part_name, "wb")) == NULL) {
        fprintf (stderr, "Cannot write to \"%s\"!\n", part_name);
        out = EXIT_FAILURE;
      } else {
        if (!tracks)
          printf ("Writing part %d (%s)...", cur_part, part_name);
        else
          printf ("Writing part %d (%s - Tracks %d to %d)...", cur_part,
                  part_name, (cur_part - 1) * tracks,
                  cur_part * tracks - 1 > 79 ? 79 : cur_part * tracks - 1);

        /* If there's any valid data in the buffer, write them */
        if (buf_length > 0) {
          i = BUF_SIZE - buf_length;
          fwrite (b + buf_length, sizeof (char), i, f_out);
        } else
          i = 0;

        for (; i < part_size &&
             (r = fread (b, sizeof (unsigned char), BUF_SIZE, f_in)) > 0;
             i += r) {
          buf_length = r;

          /* Do not write the whole buffer if exceeding the part size */
          if (i + r > part_size)
            buf_length = part_size - i;

          fwrite (b, sizeof (unsigned char), buf_length, f_out);
        }

        fclose (f_out);

        /* Part end */
        printf (" done");
        if (i < part_size)
           printf (" (%d bytes only)", i);
           printf ("!\n");
      }
    }
  }

  /* Free used memory */
  free (b);

  return (out);
}

Inviato: sab 8 ott 2005, 13:38
da Teo
Con cosa posso compilarlo per DOS? Portate pazienza per l'ignoranza...
Grazie mille comunque!

Inviato: sab 8 ott 2005, 14:03
da gallows
Teo ha scritto:Con cosa posso compilarlo per DOS? Portate pazienza per l'ignoranza...
Grazie mille comunque!
gcc esiste anche per windows.

Inviato: sab 8 ott 2005, 14:54
da useless
xké compilarlo x dos? l'ho scritto in dos ma è ansi c e compila ovunque.
comunque usa lo split che c'è già in slackware, è sicuramente + funzionale.