         {
             reverse = false;
         }
-    }
-
-    FILE* output_file = fopen( out_name, "w" );
+        else if (strcmp( argv[i], "-a" ) == 0 ||
+                 strcmp( argv[i], "--ascii" ) == 0)  // ascii format
+        {
+            format = ASCII;
+        }
+        else if (strcmp( argv[i], "-p2" ) == 0 ||
+                 strcmp( argv[i], "--packed-2" ) == 0)  // 2-bits packed
+        {
+            format = PACKED2;
+        }
+        else if (strcmp( argv[i], "-p4" ) == 0 ||
+                 strcmp( argv[i], "--packed-4" ) == 0)  // 4-bits packed
+        {
+            format = PACKED4;
+        }
+        else if (strcmp( argv[i], "-i" ) == 0 ||
+                 strcmp( argv[i], "--idx" ) == 0)       // index file
+        {
+            idx_name = argv[++i];
+        }
+    }
+
+    std::string out_string = out_name;
+    // parse out file extension; look for .fastq.gz, .fastq suffixes
+    uint32 len = uint32( strlen(out_name) );
+    bool is_gzipped = false;
+
+    // do we have a .gz suffix?
+    if (len >= strlen(".gz"))
+    {
+        if (strcmp(&out_name[len - strlen(".gz")], ".gz") == 0)
+        {
+            is_gzipped = true;
+            len = uint32(len - strlen(".gz"));
+        }
+    }
+
+    void* output_file  = NULL;
+    void* output_index = NULL;
+
+    if (format == ASCII)
+    {
+        // open a plain ASCII file
+        output_file = gzopen( out_name, is_gzipped ? "w1R" : "w" );
+    }
+    else
+    {
+        // open a binary file
+        output_file = gzopen( out_name, is_gzipped ? "wb1R" : "wbT" );
+    }
+
     if (output_file == NULL)
     {
         log_error(stderr, "    failed opening file \"%s\"\n", out_name);
         return 1;
+    }
+
+    if (idx_name)
+    {
+        output_index = fopen( idx_name, "wb" );
+        if (output_index == NULL)
+        {
+            log_error(stderr, "    failed opening file \"%s\"\n", idx_name);
+            return 1;
+        }
     }
 
     log_visible(stderr,"nvExtractReads... started\n");

    if (forward) encoding_flags |= io::FORWARD;
    if (reverse) encoding_flags |= io::REVERSE_COMPLEMENT;

    if (read( reads_name, output_file, qencoding, io::SequenceEncoding(encoding_flags) ) == false)
        return 1;

    fclose( output_file );

    log_visible(stderr,"nvExtractReads... done\n");
    return 0;
}


    const uint32 batch_size = 512*1024;

    std::vector<char>   char_read( 1024*1024 );
    std::vector<uint64> index( 512*1024 + 1u );

    uint64 offset = 0u;

    uint32 n_reads = 0;

