@@ -271,43 +271,58 @@ def _list_outputs(self):
271271 return outputs
272272
273273
274- class GunzipInputSpec (BaseInterfaceInputSpec ):
275- in_file = File (exists = True , mandatory = True )
274+ class GzipInputSpec (TraitedSpec ):
275+ in_file = File (exists = True , mandatory = True , desc = "file to (de)compress" )
276+ mode = traits .Enum (
277+ "compress" , "decompress" , usedefault = True , desc = "compress or decompress"
278+ )
276279
277280
278- class GunzipOutputSpec (TraitedSpec ):
279- out_file = File (exists = True )
281+ class GzipOutputSpec (TraitedSpec ):
282+ out_file = File ()
280283
281284
282- class Gunzip (BaseInterface ):
283- """Gunzip wrapper
285+ class Gzip (BaseInterface ):
286+ """Gzip wrapper
284287
285- >>> from nipype.algorithms.misc import Gunzip
286- >>> gunzip = Gunzip (in_file='tpms_msk.nii.gz')
287- >>> res = gunzip .run()
288+ >>> from nipype.algorithms.misc import Gzip
289+ >>> gzip = Gzip (in_file='tpms_msk.nii.gz', mode="decompress" )
290+ >>> res = gzip .run()
288291 >>> res.outputs.out_file # doctest: +ELLIPSIS
289292 '.../tpms_msk.nii'
290293
294+ >>> gzip = Gzip(in_file='tpms_msk.nii')
295+ >>> res = gzip.run()
296+ >>> res.outputs.out_file # doctest: +ELLIPSIS
297+ '.../tpms_msk.nii.gz'
298+
291299 .. testcleanup::
292300
293301 >>> os.unlink('tpms_msk.nii')
294302 """
295303
296- input_spec = GunzipInputSpec
297- output_spec = GunzipOutputSpec
304+ input_spec = GzipInputSpec
305+ output_spec = GzipOutputSpec
298306
299307 def _gen_output_file_name (self ):
300308 _ , base , ext = split_filename (self .inputs .in_file )
301- if ext [- 3 :].lower () == ".gz" :
309+ if self . inputs . mode == "decompress" and ext [- 3 :].lower () == ".gz" :
302310 ext = ext [:- 3 ]
311+ elif self .inputs .mode == "compress" :
312+ ext = f"{ ext } .gz"
303313 return os .path .abspath (base + ext )
304314
305315 def _run_interface (self , runtime ):
306316 import gzip
307317 import shutil
308318
309- with gzip .open (self .inputs .in_file , "rb" ) as in_file :
310- with open (self ._gen_output_file_name (), "wb" ) as out_file :
319+ if self .inputs .mode == "compress" :
320+ open_input , open_output = open , gzip .open
321+ else :
322+ open_input , open_output = gzip .open , open
323+
324+ with open_input (self .inputs .in_file , "rb" ) as in_file :
325+ with open_output (self ._gen_output_file_name (), "wb" ) as out_file :
311326 shutil .copyfileobj (in_file , out_file )
312327 return runtime
313328
@@ -317,6 +332,27 @@ def _list_outputs(self):
317332 return outputs
318333
319334
335+ class GunzipInputSpec (GzipInputSpec ):
336+ mode = traits .Enum ("decompress" , usedefault = True , desc = "decompress or compress" )
337+
338+
339+ class Gunzip (Gzip ):
340+ """Gunzip wrapper
341+
342+ >>> from nipype.algorithms.misc import Gunzip
343+ >>> gunzip = Gunzip(in_file='tpms_msk.nii.gz')
344+ >>> res = gunzip.run()
345+ >>> res.outputs.out_file # doctest: +ELLIPSIS
346+ '.../tpms_msk.nii'
347+
348+ .. testcleanup::
349+
350+ >>> os.unlink('tpms_msk.nii')
351+ """
352+
353+ input_spec = GunzipInputSpec
354+
355+
320356def replaceext (in_list , ext ):
321357 out_list = list ()
322358 for filename in in_list :
0 commit comments