एंड्रॉइड में प्रोग्राम को फ़ाइलों को अनज़िप कैसे करें?


131

मुझे एक छोटे कोड स्निपेट की आवश्यकता है जो किसी दिए गए .zip फ़ाइल से कुछ फ़ाइलों को अनज़िप करता है और ज़िप की गई फ़ाइल के प्रारूप के अनुसार अलग-अलग फाइलें देता है। कृपया अपना ज्ञान पोस्ट करें और मेरी मदद करें।


जवाबों:


140

पेनो के संस्करण को थोड़ा अनुकूलित किया गया था। प्रदर्शन में वृद्धि बोधगम्य है।

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         String filename;
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;
         byte[] buffer = new byte[1024];
         int count;

         while ((ze = zis.getNextEntry()) != null) 
         {
             filename = ze.getName();

             // Need to create directories if not exists, or
             // it will generate an Exception...
             if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
             }

             FileOutputStream fout = new FileOutputStream(path + filename);

             while ((count = zis.read(buffer)) != -1) 
             {
                 fout.write(buffer, 0, count);             
             }

             fout.close();               
             zis.closeEntry();
         }

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}

12
<उपयोग-अनुमति एंड्रॉइड: नाम = "android.permission.WRITE_EXTERNAL_STORAGE" />
लो मोर्डा

1
मुझे लगता है कि हाँ, यह काम करता है, क्योंकि यह चीजों को खोलना बहुत सामान्य तरीका है। बस सही 'पथ' और 'ज़िपनाम' प्राप्त करने का प्रबंधन करें। मैंने कुछ ऐसे सामान भी देखे हैं जिनमें आपकी रुचि हो सकती है (सुनिश्चित करें कि आपने इसे पहले ही देख लिया है): लिंक
वसीली सोचिंस्की

1
क्योंकि आपको "फ़ाइल-ओनली" ऑपरेशन को छोड़ना होगा, यदि आपकी zeनिर्देशिका है। इन ऑपरेशनों को करने की कोशिश एक अपवाद का कारण बनेगी।
वासिली सोचिंस्की

1
यह जवाब काम नहीं करना चाहिए, क्योंकि यह उस पर डेटा लिखने के लिए लापता फ़ाइलों को नहीं बनाता है !!
उमर हुसाम एल्दिन

1
वास्तव में, यह कोड काम नहीं करेगा यदि ज़िप फ़ाइल को रद्दी पथ के बिना बनाया गया है, उदाहरण के लिए, आप इस कोड को एपीके फ़ाइल को अनज़िप करने के लिए चला सकते हैं, तो आपको फाइलनॉटफ़ाउंड एक्ससेप्शन मिलेगा।
शॉ

99

वसीली सोचिंस्की के उत्तर के आधार पर एक छोटा और ठीक किया गया

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(
            new BufferedInputStream(new FileInputStream(zipFile)));
    try {
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " +
                        dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            /* if time should be restored as well
            long time = ze.getTime();
            if (time > 0)
                file.setLastModified(time);
            */
        }
    } finally {
        zis.close();
    }
}

उल्लेखनीय अंतर

  • public static - यह एक स्थिर उपयोगिता विधि है जो कहीं भी हो सकती है।
  • 2 Fileपैरामीटर क्योंकि Stringहैं: / फ़ाइलों के लिए और कोई निर्दिष्ट नहीं कर सकता कि ज़िप फ़ाइल को पहले कहां निकाला जाना है। साथ ही path + filenameकंसट्रक्शन> https://stackoverflow.com/a/412495/995891
  • throws- क्योंकि देर से पकड़ - अगर वास्तव में उन में रुचि नहीं है एक कोशिश पकड़ जोड़ें।
  • वास्तव में यह सुनिश्चित करता है कि सभी मामलों में आवश्यक निर्देशिकाएं मौजूद हैं। हर ज़िप में फ़ाइल प्रविष्टियों के अग्रिम में सभी आवश्यक निर्देशिका प्रविष्टियाँ नहीं होती हैं। इसमें 2 संभावित बग थे:
    • यदि ज़िप में एक खाली निर्देशिका है और परिणामस्वरूप निर्देशिका के बजाय एक मौजूदा फ़ाइल है, तो इसे अनदेखा कर दिया गया था। का वापसी मूल्य mkdirs()महत्वपूर्ण है।
    • ज़िप फ़ाइलों पर क्रैश हो सकता है जिसमें निर्देशिकाएं नहीं हैं।
  • बढ़ा हुआ बफ़र आकार बढ़ाएँ, इससे प्रदर्शन में थोड़ा सुधार होना चाहिए। भंडारण आमतौर पर 4k ब्लॉकों में होता है और छोटे टुकड़ों में लिखना आमतौर पर आवश्यक से धीमा होता है।
  • finallyसंसाधन लीक को रोकने के लिए जादू का उपयोग करता है ।

इसलिए

unzip(new File("/sdcard/pictures.zip"), new File("/sdcard"));

मूल के बराबर करना चाहिए

unpackZip("/sdcard/", "pictures.zip")

हैलो im sdcard / temp / 768 \ 769.json जैसे पिछड़े स्लैश के साथ रास्ता पा रहा है, इसलिए मुझे त्रुटि हो रही है आप मुझे बता सकते हैं कि इसे कैसे प्रबंधित किया जाए
Ando Masahashi

@AndoMasahashi जो एक लिनक्स फाइल सिस्टम पर एक कानूनी फ़ाइल नाम होना चाहिए। आपको क्या त्रुटि मिलती है और अंत में फ़ाइल नाम कैसा दिखना चाहिए?
zapl

यह /sdcard/pictures\picturess.jpeg की तरह दिखता है और त्रुटि फ़ाइल त्रुटि नहीं मिली
Ando Masahashi

यह ठीक काम करता है, लेकिन यह अपवाद को फेंक देता है जब ज़िप के अंदर फ़ाइल नाम में से एक नहीं होता है UTF8 format। इसलिए, मैंने इस कोड का उपयोग किया, जो अपाचे के उपयोग करता है commons-compress
आशीष तन्ना

@ आशीषन्ना वास्तव में, यह एक ज्ञात समस्या है blogs.oracle.com/xuemingshen/entry/non_utf_8_encoding_in
zapl

26

यह मेरी अनज़िप विधि है, जिसका मैं उपयोग करता हूं:

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));          
         ZipEntry ze;

         while((ze = zis.getNextEntry()) != null) 
         {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             byte[] buffer = new byte[1024];
             int count;

             String filename = ze.getName();
             FileOutputStream fout = new FileOutputStream(path + filename);

             // reading and writing
             while((count = zis.read(buffer)) != -1) 
             {
                 baos.write(buffer, 0, count);
                 byte[] bytes = baos.toByteArray();
                 fout.write(bytes);             
                 baos.reset();
             }

             fout.close();               
             zis.closeEntry();
         }

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}

क्या आपको लगता है कि समान कोड विस्तार के लिए unzipping या unpacking के लिए काम करता है APK विस्तार फाइलें obb files?
LOG_TAG

13

एंड्रॉइड में बिल्ड-इन जावा एपीआई है। की जाँच करें java.util.zip पैकेज।

वर्ग ZipInputStream वह है जिसे आपको देखना चाहिए। ZipInputStream से ZipEntry पढ़ें और इसे फाइलसिस्टम / फ़ोल्डर में डंप करें। ज़िप फ़ाइल में संपीड़ित करने के लिए समान उदाहरण की जाँच करें ।


6
आपको एक कोड उदाहरण प्रदान करना चाहिए था। आप बहुत सारे बिंदुओं से चूक गए।
कैमरन लोवेल पामर

10

कोटलिन रास्ता

//FileExt.kt

data class ZipIO (val entry: ZipEntry, val output: File)

fun File.unzip(unzipLocationRoot: File? = null) {

    val rootFolder = unzipLocationRoot ?: File(parentFile.absolutePath + File.separator + nameWithoutExtension)
    if (!rootFolder.exists()) {
       rootFolder.mkdirs()
    }

    ZipFile(this).use { zip ->
        zip
        .entries()
        .asSequence()
        .map {
            val outputFile = File(rootFolder.absolutePath + File.separator + it.name)
            ZipIO(it, outputFile)
        }
        .map {
            it.output.parentFile?.run{
                if (!exists()) mkdirs()
            }
            it
        }
        .filter { !it.entry.isDirectory }
        .forEach { (entry, output) ->
            zip.getInputStream(entry).use { input ->
                output.outputStream().use { output ->
                    input.copyTo(output)
                }
            }
        }
    }

}

प्रयोग

val zipFile = File("path_to_your_zip_file")
file.unzip()

7

जबकि पहले से ही यहां जो उत्तर हैं वे अच्छी तरह से काम करते हैं, मैंने पाया कि वे मुझसे उम्मीद की तुलना में थोड़ा धीमा थे। इसके बजाय मैंने zip4j का उपयोग किया , जो मुझे लगता है कि इसकी गति के कारण सबसे अच्छा समाधान है। यह संपीड़न की मात्रा के लिए विभिन्न विकल्पों के लिए भी अनुमति देता है, जो मुझे उपयोगी लगा।


6

अद्यतन 2016 निम्न वर्ग का उपयोग करें

    package com.example.zip;

    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import android.util.Log;

    public class DecompressFast {



 private String _zipFile; 
  private String _location; 

  public DecompressFast(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 

    _dirChecker(""); 
  } 

  public void unzip() { 
    try  { 
      FileInputStream fin = new FileInputStream(_zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
        Log.v("Decompress", "Unzipping " + ze.getName()); 

        if(ze.isDirectory()) { 
          _dirChecker(ze.getName()); 
        } else { 
          FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
         BufferedOutputStream bufout = new BufferedOutputStream(fout);
          byte[] buffer = new byte[1024];
          int read = 0;
          while ((read = zin.read(buffer)) != -1) {
              bufout.write(buffer, 0, read);
          }




          bufout.close();

          zin.closeEntry(); 
          fout.close(); 
        } 

      } 
      zin.close(); 


      Log.d("Unzip", "Unzipping complete. path :  " +_location );
    } catch(Exception e) { 
      Log.e("Decompress", "unzip", e); 

      Log.d("Unzip", "Unzipping failed");
    } 

  } 

  private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 

    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 


 }

कैसे इस्तेमाल करे

 String zipFile = Environment.getExternalStorageDirectory() + "/the_raven.zip"; //your zip file location
    String unzipLocation = Environment.getExternalStorageDirectory() + "/unzippedtestNew/"; // destination folder location
  DecompressFast df= new DecompressFast(zipFile, unzipLocation);
    df.unzip();

अनुमतियां

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

फ़ाइल का नाम देख सकते हैं, लेकिन फ़ाइल को निकालने की कोशिश करते समय, मुझे FileNotFoundException त्रुटि मिल रही है
पार्थ

5

@Zapl जवाब के अनुसार, प्रगति रिपोर्ट के साथ अनज़िप करें:

public interface UnzipFile_Progress
{
    void Progress(int percent, String FileName);
}

// unzip(new File("/sdcard/pictures.zip"), new File("/sdcard"));
public static void UnzipFile(File zipFile, File targetDirectory, UnzipFile_Progress progress) throws IOException,
        FileNotFoundException
{
    long total_len = zipFile.length();
    long total_installed_len = 0;

    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try
    {
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[1024];
        while ((ze = zis.getNextEntry()) != null)
        {
            if (progress != null)
            {
                total_installed_len += ze.getCompressedSize();
                String file_name = ze.getName();
                int percent = (int)(total_installed_len * 100 / total_len);
                progress.Progress(percent, file_name);
            }

            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try
            {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally
            {
                fout.close();
            }

            // if time should be restored as well
            long time = ze.getTime();
            if (time > 0)
                file.setLastModified(time);
        }
    } finally
    {
        zis.close();
    }
}

3
public class MainActivity extends Activity {

private String LOG_TAG = MainActivity.class.getSimpleName();

private File zipFile;
private File destination;

private TextView status;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    status = (TextView) findViewById(R.id.main_status);
    status.setGravity(Gravity.CENTER);

    if ( initialize() ) {
        zipFile = new File(destination, "BlueBoxnew.zip");
        try {
            Unzipper.unzip(zipFile, destination);
            status.setText("Extracted to \n"+destination.getAbsolutePath());
        } catch (ZipException e) {
            Log.e(LOG_TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(LOG_TAG, e.getMessage());
        }
    } else {
        status.setText("Unable to initialize sd card.");
    }
}

public boolean initialize() {
    boolean result = false;
     File sdCard = new File(Environment.getExternalStorageDirectory()+"/zip/");
    //File sdCard = Environment.getExternalStorageDirectory();
    if ( sdCard != null ) {
        destination = sdCard;
        if ( !destination.exists() ) {
            if ( destination.mkdir() ) {
                result = true;
            }
        } else {
            result = true;
        }
    }

    return result;
}

 }

-> हेल्पर क्लास (Unzipper.java)

    import java.io.File;
    import java.io.FileInputStream;
   import java.io.FileOutputStream;
    import java.io.IOException;
       import java.util.zip.ZipEntry;
    import java.util.zip.ZipException;
    import java.util.zip.ZipInputStream;
     import android.util.Log;

   public class Unzipper {

private static String LOG_TAG = Unzipper.class.getSimpleName();

public static void unzip(final File file, final File destination) throws ZipException, IOException {
    new Thread() {
        public void run() {
            long START_TIME = System.currentTimeMillis();
            long FINISH_TIME = 0;
            long ELAPSED_TIME = 0;
            try {
                ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
                String workingDir = destination.getAbsolutePath()+"/";

                byte buffer[] = new byte[4096];
                int bytesRead;
                ZipEntry entry = null;
                while ((entry = zin.getNextEntry()) != null) {
                    if (entry.isDirectory()) {
                        File dir = new File(workingDir, entry.getName());
                        if (!dir.exists()) {
                            dir.mkdir();
                        }
                        Log.i(LOG_TAG, "[DIR] "+entry.getName());
                    } else {
                        FileOutputStream fos = new FileOutputStream(workingDir + entry.getName());
                        while ((bytesRead = zin.read(buffer)) != -1) {
                            fos.write(buffer, 0, bytesRead);
                        }
                        fos.close();
                        Log.i(LOG_TAG, "[FILE] "+entry.getName());
                    }
                }
                zin.close();

                FINISH_TIME = System.currentTimeMillis();
                ELAPSED_TIME = FINISH_TIME - START_TIME;
                Log.i(LOG_TAG, "COMPLETED in "+(ELAPSED_TIME/1000)+" seconds.");
            } catch (Exception e) {
                Log.e(LOG_TAG, "FAILED");
            }
        };
    }.start();
}

   }

-> xml लेआउट (activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity" >

<TextView
    android:id="@+id/main_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

</RelativeLayout>

-> मेनिफेस्ट फ़ाइल में अनुमति:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2

यहाँ एक ZipFileIterator है (एक जावा Iterator की तरह, लेकिन ज़िप फ़ाइलों के लिए):

package ch.epfl.bbp.io;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipFileIterator implements Iterator<File> {

    private byte[] buffer = new byte[1024];

    private FileInputStream is;
    private ZipInputStream zis;
    private ZipEntry ze;

    public ZipFileIterator(File file) throws FileNotFoundException {
    is = new FileInputStream(file);
    zis = new ZipInputStream(new BufferedInputStream(is));
    }

    @Override
    public boolean hasNext() {
    try {
        return (ze = zis.getNextEntry()) != null;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
    }

    @Override
    public File next() {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int count;

        String filename = ze.getName();
        File tmpFile = File.createTempFile(filename, "tmp");
        tmpFile.deleteOnExit();// TODO make it configurable
        FileOutputStream fout = new FileOutputStream(tmpFile);

        while ((count = zis.read(buffer)) != -1) {
        baos.write(buffer, 0, count);
        byte[] bytes = baos.toByteArray();
        fout.write(bytes);
        baos.reset();
        }
        fout.close();
        zis.closeEntry();

        return tmpFile;

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    }

    @Override
    public void remove() {
    throw new RuntimeException("not implemented");
    }

    public void close() {
    try {
        zis.close();
        is.close();
    } catch (IOException e) {// nope
    }
    }
}

क्या आपको लगता है कि समान कोड विस्तार के लिए unzipping या unpacking के लिए काम करता है APK विस्तार फाइलें obb files?
LOG_TAG

2

न्यूनतम उदाहरण मैंने अपने एप्लिकेशन कैश फ़ोल्डर में अपने जिपफाइल से एक विशिष्ट फ़ाइल को अनज़िप करने के लिए उपयोग किया। मैंने तब एक अलग विधि का उपयोग करते हुए मैनिफ़ेस्ट फ़ाइल को पढ़ा।

private void unzipUpdateToCache() {
    ZipInputStream zipIs = new ZipInputStream(context.getResources().openRawResource(R.raw.update));
    ZipEntry ze = null;

    try {

        while ((ze = zipIs.getNextEntry()) != null) {
            if (ze.getName().equals("update/manifest.json")) {
                FileOutputStream fout = new FileOutputStream(context.getCacheDir().getAbsolutePath() + "/manifest.json");

                byte[] buffer = new byte[1024];
                int length = 0;

                while ((length = zipIs.read(buffer))>0) {
                    fout.write(buffer, 0, length);
                }
                zipIs .closeEntry();
                fout.close();
            }
        }
        zipIs .close();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

2

मैं जिप फाइलों के साथ काम कर रहा हूं जिन्हें जावा का जिपफाइल क्लास हैंडल नहीं कर पा रहा है। जावा 8 जाहिरा तौर पर संपीड़न विधि 12 (bzip2 मुझे विश्वास है) को संभाल नहीं सकता है। Zip4j सहित कई तरीकों की कोशिश करने के बाद (जो एक अन्य मुद्दे के कारण इन विशेष फ़ाइलों के साथ भी विफल रहता है), मुझे अपाचे के कॉमन्स-सेक के साथ सफलता मिली जो अतिरिक्त संपीड़न विधियों का समर्थन करता है जैसा कि यहां बताया गया है

ध्यान दें कि नीचे ZIPFile वर्ग java.util.zip से नहीं है।

यह वास्तव में org.apache.commons.compress.archivers.zip.ZipFile है इसलिए आयातों से सावधान रहें।

try (ZipFile zipFile = new ZipFile(archiveFile)) {
    Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        File entryDestination = new File(destination, entry.getName());
        if (entry.isDirectory()) {
            entryDestination.mkdirs();
        } else {
            entryDestination.getParentFile().mkdirs();
            try (InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination)) {
                IOUtils.copy(in, out);
            }
        }
    }
} catch (IOException ex) {
    log.debug("Error unzipping archive file: " + archiveFile, ex);
}

ग्रेडेल के लिए:

compile 'org.apache.commons:commons-compress:1.18'

2

Zapl के उत्तर के आधार पर, try()चारों ओर जोड़ने से Closeableउपयोग के बाद स्वचालित रूप से धाराएं बंद हो जाती हैं।

public static void unzip(File zipFile, File targetDirectory) {
    try (FileInputStream fis = new FileInputStream(zipFile)) {
        try (BufferedInputStream bis = new BufferedInputStream(fis)) {
            try (ZipInputStream zis = new ZipInputStream(bis)) {
                ZipEntry ze;
                int count;
                byte[] buffer = new byte[Constant.DefaultBufferSize];
                while ((ze = zis.getNextEntry()) != null) {
                    File file = new File(targetDirectory, ze.getName());
                    File dir = ze.isDirectory() ? file : file.getParentFile();
                    if (!dir.isDirectory() && !dir.mkdirs())
                        throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
                    if (ze.isDirectory())
                        continue;
                    try (FileOutputStream fout = new FileOutputStream(file)) {
                        while ((count = zis.read(buffer)) != -1)
                            fout.write(buffer, 0, count);
                    }
                }
            }
        }
    } catch (Exception ex) {
        //handle exception
    }
}

जॉन स्कीट के उत्तर से Stream.CopyTo से प्राप्त Constant.DefaultBufferSize( 65536) का उपयोग करना : https://stackoverflow.com/a/411605/1876355C# .NET 4

मैं हमेशा बस byte[1024]या byte[4096]बफर का उपयोग करते हुए पोस्ट देखता हूं , कभी नहीं पता था कि यह बहुत बड़ा हो सकता है जो प्रदर्शन में सुधार करता है और अभी भी पूरी तरह से सामान्य काम कर रहा है।

यहाँ Streamस्रोत कोड है: https://referencesource.microsoft.com/#mscorlib/system/io/stream.cs

//We pick a value that is the largest multiple of 4096 that is still smaller than the large object heap threshold (85K).
// The CopyTo/CopyToAsync buffer is short-lived and is likely to be collected at Gen0, and it offers a significant
// improvement in Copy performance.

private const int _DefaultCopyBufferSize = 81920;

हालाँकि, मैंने इसे वापस डायल कर दिया, 65536जो कि 4096सुरक्षित होने के लिए भी बहुविध है ।


1
यह इस धागे में सबसे अच्छा समाधान है। इसके अलावा, मैं भी FileOutputStream के साथ स्टैक में BufferedOutputStream का उपयोग करूँगा।
MarkoR

1

पासवर्ड संरक्षित ज़िप फ़ाइल

यदि आप पासवर्ड के साथ फ़ाइलों को संपीड़ित करना चाहते हैं, तो आप इस लाइब्रेरी पर एक नज़र डाल सकते हैं, जो फ़ाइलों को पासवर्ड से आसानी से ज़िप कर सकते हैं:

ज़िप:

ZipArchive zipArchive = new ZipArchive();
zipArchive.zip(targetPath,destinationPath,password);

unzip:

ZipArchive zipArchive = new ZipArchive();
zipArchive.unzip(targetPath,destinationPath,password);

rar:

RarArchive rarArchive = new RarArchive();
rarArchive.extractArchive(file archive, file destination);

इस पुस्तकालय का प्रलेखन काफी अच्छा है, मैंने अभी वहां से कुछ उदाहरण जोड़े हैं। यह पूरी तरह से मुफ़्त है और विशेष रूप से Android के लिए लिखा गया है।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.