diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php
index c1a4254c8f..9b84dc46b2 100644
--- a/lib/filestorage/stored_file.php
+++ b/lib/filestorage/stored_file.php
@@ -537,7 +537,12 @@ class stored_file {
      * @return bool success
      */
     public function archive_file(file_archive $filearch, $archivepath) {
-        return $this->filesystem->add_storedfile_to_archive($this, $filearch, $archivepath);
+        if ($this->get_repository_type() == 'filesystem') {
+            return $this->repository->add_storedfile_to_archive($this, $filearch, $archivepath);
+        } else {
+            return $this->filesystem->add_storedfile_to_archive($this, $filearch, $archivepath);
+        }
+
     }

     /**
diff --git a/repository/filesystem/lib.php b/repository/filesystem/lib.php
index ce8502fe9e..e83ceba04d 100644
--- a/repository/filesystem/lib.php
+++ b/repository/filesystem/lib.php
@@ -813,6 +813,17 @@ class repository_filesystem extends repository {
     public function supports_relative_file() {
         return $this->get_option('relativefiles');
     }
+
+    public function add_storedfile_to_archive(stored_file $file, file_archive $filearch, $archivepath) {
+        if ($file->is_directory()) {
+            return $filearch->add_directory($archivepath);
+        } else {
+            $reference = $file->get_reference();
+            $f = $this->get_rootpath() . ltrim($reference, '/');
+            $filename = $file->get_filename();
+            return $filearch->add_file_from_string($filename, file_get_contents($f));
+        }
+    }
 }

 /**
