Use the archive utility for fetch_cvd extracting.

Test: Run fetch_cvd
Bug: 135293952
Change-Id: I094f38b89a8539a9c0245d9a1798080ad944d716
diff --git a/host/commands/fetcher/fetch_cvd.cc b/host/commands/fetcher/fetch_cvd.cc
index 5f60700..eab9a63 100644
--- a/host/commands/fetcher/fetch_cvd.cc
+++ b/host/commands/fetcher/fetch_cvd.cc
@@ -21,6 +21,7 @@
 #include "gflags/gflags.h"
 #include <glog/logging.h>
 
+#include "common/libs/utils/archive.h"
 #include "common/libs/utils/files.h"
 #include "common/libs/utils/subprocess.h"
 
@@ -149,14 +150,8 @@
     return false;
   }
 
-  cvd::Command tar_cmd("/bin/tar");
-  tar_cmd.AddParameter("xvf");
-  tar_cmd.AddParameter(local_path);
-  tar_cmd.AddParameter("-C");
-  tar_cmd.AddParameter(target_directory);
-  tar_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut,
-                        cvd::Subprocess::StdIOChannel::kStdErr);
-  if (tar_cmd.Start().Wait() != 0) {
+  cvd::Archive archive(local_path);
+  if (!archive.ExtractAll(target_directory)) {
     LOG(FATAL) << "Could not extract " << local_path;
     return false;
   }
@@ -206,17 +201,8 @@
     LOG(FATAL) << "Could not create " << otatools_dir;
     return false;
   }
-  cvd::Command bsdtar_cmd("/usr/bin/bsdtar");
-  bsdtar_cmd.AddParameter("-x");
-  bsdtar_cmd.AddParameter("-v");
-  bsdtar_cmd.AddParameter("-C");
-  bsdtar_cmd.AddParameter(otatools_dir);
-  bsdtar_cmd.AddParameter("-f");
-  bsdtar_cmd.AddParameter(local_path);
-  bsdtar_cmd.AddParameter("-S");
-  bsdtar_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut,
-                           cvd::Subprocess::StdIOChannel::kStdErr);
-  if (bsdtar_cmd.Start().Wait() != 0) {
+  cvd::Archive archive(local_path);
+  if (!archive.ExtractAll(otatools_dir)) {
     LOG(FATAL) << "Could not extract " << local_path;
     return false;
   }
diff --git a/host/commands/fetcher/install_zip.cc b/host/commands/fetcher/install_zip.cc
index b5b8f81..a468b2e 100644
--- a/host/commands/fetcher/install_zip.cc
+++ b/host/commands/fetcher/install_zip.cc
@@ -22,48 +22,25 @@
 
 #include <glog/logging.h>
 
-#include "common/libs/strings/str_split.h"
+#include "common/libs/utils/archive.h"
 #include "common/libs/utils/subprocess.h"
 
-namespace {
-
-std::vector<std::string> ArchiveContents(const std::string& archive) {
-  std::string bsdtar_output;
-  auto bsdtar_ret =
-      cvd::execute_capture_output({"/usr/bin/bsdtar", "-tf", archive},
-                                  &bsdtar_output);
-  return bsdtar_ret == 0
-      ? cvd::StrSplit(bsdtar_output, '\n')
-      : std::vector<std::string>();
-}
-
-} // namespace
-
-bool ExtractImages(const std::string& archive,
+bool ExtractImages(const std::string& archive_file,
                    const std::string& target_directory,
                    const std::vector<std::string>& images) {
-  cvd::Command bsdtar_cmd("/usr/bin/bsdtar");
-  bsdtar_cmd.AddParameter("-x");
-  bsdtar_cmd.AddParameter("-v");
-  bsdtar_cmd.AddParameter("-C");
-  bsdtar_cmd.AddParameter(target_directory);
-  bsdtar_cmd.AddParameter("-f");
-  bsdtar_cmd.AddParameter(archive);
-  bsdtar_cmd.AddParameter("-S");
-  for (const auto& img : images) {
-    bsdtar_cmd.AddParameter(img);
-  }
-  bsdtar_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut,
-                           cvd::Subprocess::StdIOChannel::kStdErr);
-  auto bsdtar_ret = bsdtar_cmd.Start().Wait();
-  if (bsdtar_ret != 0) {
-    LOG(ERROR) << "Unable to extract images. bsdtar returned " << bsdtar_ret;
+  cvd::Archive archive(archive_file);
+  bool extracted =
+      images.size() > 0
+          ? archive.ExtractFiles(images, target_directory)
+          : archive.ExtractAll(target_directory);
+  if (!extracted) {
+    LOG(ERROR) << "Unable to extract images.";
     return false;
   }
 
   bool extraction_success = true;
   std::vector<std::string> files =
-      images.size() > 0 ? images : ArchiveContents(archive);
+      images.size() > 0 ? images : archive.Contents();
   for (const auto& file : files) {
     if (file.find(".img") == std::string::npos) {
       continue;