+ char filename[PATH_MAX];
+ char *filename_c, *dirn;
+ int fd;
+ int remaining;
+ int ret;
+
+ strcpy(filename, "extracted/");
+ strcat(filename, fname);
+
+ if ((filename_c = strdup(filename)) == NULL) {
+ perror("strdup");
+ exit(1);
+ }
+ dirn = dirname(filename_c);
+ mkdir_p(dirn);
+ free(filename_c);
+
+ if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) {
+ fprintf(stderr, "%s: ", filename);
+ perror("open");
+ exit(1);
+ }
+
+ remaining = len;
+
+ while(remaining) {
+ ret = write(fd, buf + (len - remaining), remaining);
+ if (ret < 0) {
+ perror("write");
+ exit(1);
+ }
+ remaining -= ret;
+ }
+
+ printf(", %s written.\n", filename);
+
+ close(fd);